【问题标题】:Cross-Origin Request Blocked when Geting XML data using ajax使用 ajax 获取 XML 数据时跨域请求被阻止
【发布时间】:2016-12-10 02:27:59
【问题描述】:

我正在开发混合应用程序,我想让我的应用程序动态化。所以我的数据 url 为 xml 格式。但是当我尝试获取数据时它失败了。所以请帮我解决问题。

 <!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

内部调用数据

<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 未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现乱码。页面的字符编码必须在文档或传输协议中声明。文本.html 跨域请求被阻止:同源策略不允许读取位于http://wetexmobapp.ourdemopage.com/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。

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

【问题讨论】:

  • 您是从这个http://wetexmobapp.ourdemopage.com/ 域还是其他域请求ajax 调用。
  • 是的。我们已经在这个链接中有xml数据我想从这个链接中获取数据
  • 我的意思是你在这里显示的html代码是http://wetexmobapp.ourdemopage.com/
  • 不,它保存在我的系统中。xml 数据在该链接中。如果您将该链接粘贴到浏览器中,您可以看到数据。 html 文件存储在我的系统中,它在我的浏览器中本地运行
  • 您是否在控制台中看到错误日志。请张贴在这里。

标签: javascript html ajax xml


【解决方案1】:

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

【讨论】:

  • 我从 w3schools.com 获得此代码。我对 ajax 没有任何了解。我在该 xml 链接中有一些数据。所以请帮助我从该链接获取数据
猜你喜欢
  • 2019-11-04
  • 2019-04-17
  • 1970-01-01
  • 2015-06-08
  • 2019-08-01
  • 2015-05-05
  • 2018-09-09
  • 2021-06-26
相关资源
最近更新 更多