它总是在错误块中。我正在对一个不同的PORT(同一域)进行AJAX调用。
但是当我试图在新标签中点击相同的URL。
解决方案任何帮助将非常受欢迎。 div>
您可以使用JSONP作为Gaurav Agrawal建议或您可以为接收ajax请求的网站启用Access-Control-Allow-Origin。
Ajax工作像这样:相同的域但不同的端口=不同的域
如果您在ajax目标服务器上使用asp,您可以启用访问控制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: 'XXXXX.desktop.XXXX: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 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调用?
发布评论