跨域AJAX调用? [英] Cross Domain AJAX Call?

查看:91
本文介绍了跨域AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $。ajax({

url:'https://XXXX.desktop.XXXX.com:9011/iws-merchant/XXXXX.htm' ,
dataType:jsonp,
success:function(response){
str = response;

},
错误:function {
alert(ERROR:+ JSON.stringify);
}
});

它总是在错误块中。我正在对一个不同的PORT(同一域)进行AJAX调用。



但是当我试图在新标签中点击相同的URL。

解决方案



任何帮助将非常受欢迎。 div>

您可以使用JSONP作为Gaurav Agrawal建议或您可以为接收ajax请求的网站启用Access-Control-Allow-Origin。



Ajax工作像这样:
相同的域但不同的端口=不同的域



如果您在ajax目标服务器上使用asp.net,您可以启用访问控制web.config:

 < system.webServer> 
< httpProtocol>
< customHeaders>
< add name =Access-Control-Allow-Originvalue =*/>
< / customHeaders>
< / httpProtocol>
< system.webServer>

,并为自己一个好处,并将*替换为您的网站网址!



在某些情况下,你甚至可以需要这些键,只需google添加它之前的每个函数!

 < add name =Access-Control-Allow-Headersvalue =*/> 
< add name =Access-Control-Allow-Methodsvalue =*/>
< add name =Access-Control-Allow-Credentialsvalue =true/>
< add name =Access-Control-Expose-Headersvalue =*/>


$.ajax({

      url: 'https://XXXXX.desktop.XXXX.com:9011/iws-merchant/XXXXX.htm',
      dataType: "jsonp",
      success: function (response) {
       str=response;

      },
      error: function( response ) {
          alert( "ERROR:  " + JSON.stringify );
       }
   });

It is always going in error block. I am making an AJAX call to a different PORT(Same Domain).

But when i try to hit the same URL in new tab. I am able to see the response.

Any help will be highly appreicated.

解决方案

you can use JSONP as Gaurav Agrawal suggested OR you can enable the Access-Control-Allow-Origin for the site who receives ajax request.

Ajax works like this: Same domain but different port = different domain

if you are using asp.net on your ajax target server you can enable access control adding this in web.config:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
<system.webServer>

and do yourself a favor and replace "*" with your site url!

in some situation you can need even those keys, just google every function before adding it!

<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Expose-Headers" value="*"/>

这篇关于跨域AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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