使用 ajax 获取 XML 数据时跨域请求被阻止 [英] Cross-Origin Request Blocked when Geting XML data using ajax

查看:30
本文介绍了使用 ajax 获取 XML 数据时跨域请求被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <头><meta http-equiv="Access-Control-Allow-Origin" content="*"><身体><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) {//您希望允许连接的网站res.setHeader('Access-Control-Allow-Origin', 'http://localhost');//请求您希望允许的方法res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');//请求您希望允许的标头res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');//如果您需要网站在发送的请求中包含 cookie,则设置为 true//到 API(例如,如果您使用会话)res.setHeader('Access-Control-Allow-Credentials', true);//传递给下一层中间件下一个();};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';函数调用服务(){$.ajax({网址:webServiceURL,类型:POST",数据类型:xml",数据:soapMessage,过程数据:假,contentType: "text/xml; charset=\"utf-8\"",成功:OnSuccess,错误:OnError});返回假;}功能 OnSuccess(数据,状态){警报(数据.d);}函数 OnError(请求,状态,错误){警报('错误');}$(document).ready(function() {jQuery.support.cors = true;});<form method="post" action=""><div><input type="button" value="Call Web Service" onclick="CallService(); return false;"/>

</表单></html>getFloorPlanAll单击此处获取完整的操作列表.getWebsite_FloorPlanAll测试要使用 HTTP POST 协议测试操作,请单击调用"按钮.肥皂 1.1下面是一个示例 SOAP 1.1 请求和响应.显示的占位符需要替换为实际值.POST/webService/getFloorPlanAll.asmx HTTP/1.1主持人:wetexmobapp.ourdemopage.com内容类型:文本/xml;字符集=utf-8内容长度:长度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/"><肥皂:身体><getWebsite_FloorPlanAll xmlns="http://app.wetex.ae/"/></soap:Body></soap:Envelope>HTTP/1.1 200 正常内容类型:文本/xml;字符集=utf-8内容长度:长度<?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/"><肥皂:身体><getWebsite_FloorPlanAllResponse xmlns="http://app.wetex.ae/"><getWebsite_FloorPlanAllResult>xml</getWebsite_FloorPlanAllResult></getWebsite_FloorPlanAllResponse></soap:Body></soap:Envelope>肥皂 1.2下面是一个示例 SOAP 1.2 请求和响应.显示的占位符需要替换为实际值.POST/webService/getFloorPlanAll.asmx HTTP/1.1主持人:wetexmobapp.ourdemopage.com内容类型:应用程序/soap+xml;字符集=utf-8内容长度:长度<?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 正常内容类型:应用程序/soap+xml;字符集=utf-8内容长度:长度<?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 请求和响应.显示的占位符需要替换为实际值.GET/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll?HTTP/1.1主持人:wetexmobapp.ourdemopage.comHTTP/1.1 200 正常内容类型:文本/xml;字符集=utf-8内容长度:长度<?xml version="1.0"?>xmlHTTP POST以下是示例 HTTP POST 请求和响应.显示的占位符需要替换为实际值.POST/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll HTTP/1.1主持人:wetexmobapp.ourdemopage.com内容类型:应用程序/x-www-form-urlencoded内容长度:长度HTTP/1.1 200 正常内容类型:文本/xml;字符集=utf-8内容长度:长度<?xml version="1.0"?>xml

调用里面的数据

1http://wetexmobapp.ourdemopage.com/uploads/documents/floorPlan/WETEX-Floorplan-2016-0-13.pdf</FloorPlan_Url><PublishedDateTime>7/11/2016 12:00:00 AM</PublishedDateTime><CreatedDatetime>7/13/2016 5:34:00 AM</Created>活动</状态></FloorPlan></DocumentElement>

这是错误声明

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

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

解决方案

您得到的是 xhttp.readyState==4 但不是 xhttp.status == 200.您的 ajax 调用返回的状态是 0.这是主要的问题.现在,这种状态可能有很多原因.请参阅此处(HTTP 状态代码 0 - 是什么这意味着获取,还是 XMLHttpRequest?) .

I am developing hybrid app i want to make my app dynamic.so i have data url as xml format.but when i tried to get the data it is failed.so please help me to solve the problem.

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

invoked data inside

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

this is the error statement

Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead jquery.min.js:1:0 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. text.html Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://wetexmobapp.ourdemopage.com/webService/getFloorPlanAll.asmx/getWebsite_FloorPlanAll. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

i tried many steps to solve Access-Control-Allow-Origin problem but still showing same problem.

解决方案

You are getting xhttp.readyState==4 but not xhttp.status == 200. The status returned by your ajax call is 0. That's the main problem. Now there can be many reasons for this status. See here (HTTP status code 0 - what does this mean for fetch, or XMLHttpRequest?) .

这篇关于使用 ajax 获取 XML 数据时跨域请求被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆