跨域AJAX调用?

编程入门 行业动态 更新时间:2024-10-11 01:16:16
本文介绍了跨域AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

$。ajax({ url:'XXXX.desktop.XXXX: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,您可以启用访问控制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调用?

本文发布于:2023-11-28 12:44:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跨域   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!