首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ajax获取XML数据时跨域请求被阻止

使用ajax获取XML数据时跨域请求被阻止
EN

Stack Overflow用户
提问于 2016-08-04 13:46:12
回答 1查看 912关注 0票数 1

我正在开发混合应用程序,我想使我的应用程序dynamic.so,我有数据网址为xml format.but,当我试图获得的数据,它是failed.so,请帮我解决这个问题。

代码语言:javascript
运行
复制
 <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    </head>
    <body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var allowCrossDomain = function(req, res, next) {
  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost');

  // Request methods you wish to allow
 res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
};

    var webServiceURL = 'http://wetexmobapp.ourdemopage.com/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll';
    var soapMessage = '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><GetAllCategoryFamilies xmlns="http://tempuri.org/" /></soap12:Body></soap12:Envelope';

    function CallService()
    {
        $.ajax({
    url: webServiceURL, 
    type: "POST",
    dataType: "xml", 
    data: soapMessage, 
    processData: false,
    contentType: "text/xml; charset=\"utf-8\"",
    success: OnSuccess, 
    error: OnError
});

        return false;
    }

    function OnSuccess(data, status)
    {
        alert(data.d);
    }

    function OnError(request, status, error)
    {
        alert('error');
    }

    $(document).ready(function() {
        jQuery.support.cors = true;
    });
</script>

<form method="post" action="">
    <div>
        <input type="button" value="Call Web Service" onclick="CallService(); return false;" />
    </div>
</form>

</body>
</html>

getFloorPlanAll

Click here for a complete list of operations.
getWebsite_FloorPlanAll

Test
To test the operation using the HTTP POST protocol, click the 'Invoke' button.

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /webService/getFloorPlanAll.asmx HTTP/1.1
Host: wetexmobapp.ourdemopage.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://app.wetex.ae/getWebsite_FloorPlanAll"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getWebsite_FloorPlanAll xmlns="http://app.wetex.ae/" />
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getWebsite_FloorPlanAllResponse xmlns="http://app.wetex.ae/">
      <getWebsite_FloorPlanAllResult>xml</getWebsite_FloorPlanAllResult>
    </getWebsite_FloorPlanAllResponse>
  </soap:Body>
</soap:Envelope>

SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /webService/getFloorPlanAll.asmx HTTP/1.1
Host: wetexmobapp.ourdemopage.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getWebsite_FloorPlanAll xmlns="http://app.wetex.ae/" />
  </soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getWebsite_FloorPlanAllResponse xmlns="http://app.wetex.ae/">
      <getWebsite_FloorPlanAllResult>xml</getWebsite_FloorPlanAllResult>
    </getWebsite_FloorPlanAllResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP GET

The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.

GET /webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll? HTTP/1.1
Host: wetexmobapp.ourdemopage.com

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml

HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll HTTP/1.1
Host: wetexmobapp.ourdemopage.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml

内部调用的数据

代码语言:javascript
运行
复制
<DocumentElement><FloorPlan><Id>1</Id><FloorPlan_Url>http://wetexmobapp.ourdemopage.com/uploads/documents/floorPlan/WETEX-Floorplan-2016-07-13.pdf</FloorPlan_Url><PublishedDateTime>7/11/2016 12:00:00 AM</PublishedDateTime><CreatedDatetime>7/13/2016 5:34:00 AM</CreatedDatetime><Status>Active</Status></FloorPlan></DocumentElement>

这是错误语句

不推荐使用//@来指示sourceMappingURL编译指示。使用//#而不是jquery.min.js:1:0文档的字符编码未声明。如果文档包含US-ASCII范围之外的字符,则在某些浏览器配置中,该文档将以乱码文本呈现。页面的字符编码必须在文档或传输协议中声明。text.html跨域请求被阻止:同源策略不允许读取http://wetexmobapp.ourdemopage.com/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll的远程资源。(原因: CORS头'Access-Control-Allow-Origin‘缺失)。

我尝试了很多步骤来解决Access-Control-Allow-Origin问题,但仍然显示同样的问题。

EN

回答 1

Stack Overflow用户

发布于 2016-08-04 14:15:33

您得到的是xhttp.readyState==4,而不是xhttp.status == 200。ajax调用返回的状态是0。这是主要的问题。现在造成这种状态的原因可能有很多。请看这里(HTTP status code 0 - what does this mean for fetch, or XMLHttpRequest?)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38759308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档