jQuery 跨域ajax请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15477527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:08:30  来源:igfitidea点击:

Cross domain ajax request

htmlajaxjqueryasp.net-ajax

提问by Rahul_RJ

I want to get the html respond page from the cross domain url.

我想从跨域 url 获取 html 响应页面。

for this I am using the ajax request as,

为此,我使用 ajax 请求,

 $.ajax({
            type: 'GET',
            url: "http://wcidevapps.com/salescentral/idisk/0001000383/iDisk",
            dataType: "jsonp",
            success: function (response) {
                $(response).find('li a').each(function () {
                    listHref.push($(this).attr('href'));
                });

            }
        });

But after requesting it doesn't respond with any result back.

但是在请求之后它不会回复任何结果。

回答by Abhisek Das

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    function NameAFunctionName() {
        $.ajax({
          url: 'http://wcidevapps.com/salescentral/idisk/0001000383/iDisk',
          type: 'GET',
          dataType: 'json',
          headers: {
            //WRITE IF THEIR HAVE SOME HEADER REQUEST OR DATA
          },
          crossDomain: true,
          success: function (data, textStatus, xhr) {
            console.log(data);
          },
          error: function (xhr, textStatus, errorThrown) {
            console.log(errorThrown);
          }
        });
    }   
</script>

回答by radu florescu

Check documentation : http://api.jquery.com/jQuery.ajax/

检查文档:http: //api.jquery.com/jQuery.ajax/

crossDomain(default: false for same-domain requests, true for cross-domain requests)

Type: Boolean

If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

crossDomain (默认:同域请求为假,跨域请求为真)

类型:布尔型

如果您希望在同一个域上强制执行 crossDomain 请求(例如 JSONP),请将 crossDomain 的值设置为 true。例如,这允许服务器端重定向到另一个域。(版本添加:1.5)

回答by marty

My suspicion is that you see the issue because the page you're requesting does not respond with a json(p) response, but responds with a redirect to:

我怀疑您看到这个问题是因为您请求的页面没有响应 json(p) 响应,而是响应重定向到:

http://wcidevapps.com/salescentral/idisk/0001000383/iDisk/

(note the trailing slash)

(注意尾部斜线)

which then returns content type:

然后返回内容类型:

Content-Type:text/html;charset=ISO-8859-1

Edit:If your intention is to retrieve the above site's data cross-domain, for further parsing by your script, I suggest that you choose one of the following:

编辑:如果您打算跨域检索上述站点的数据,为了通过您的脚本进一步解析,我建议您选择以下之一:

Assumption 1: YOUare in control of the pages on server "http://wcidevapps.com"

假设 1控制着服务器“ http://wcidevapps.com”上的页面

In that case, you have two options: Either add CORS header "Access-Control-Allow-Origin: *" to the response (and configure the client ajax() call with dataType:"html"), or create a special JSON(P) page that delivers the same data as JSON (with padding) (and configure the client ajax() call like in the OP, with dataType:"jsonp")

在这种情况下,您有两个选择:要么将 CORS 标头“Access-Control-Allow-Origin: *”添加到响应(并使用 dataType:"html" 配置客户端 ajax() 调用),要么创建一个特殊的 JSON( P) 提供与 JSON 相同数据的页面(带填充)(并像在 OP 中一样配置客户端 ajax() 调用,使用 dataType:"jsonp")

Assumption 2: YOUare NOTin control of the pages on server http://wcidevapps.com

假设2不是在服务器上的网页控制http://wcidevapps.com

In that case, the only option I can think of is setup a proxy on a site that you control. Have that proxy "proxy" the requests/responses to "http://wcidevapps.com", but add the CORS header "Access-Control-Allow-Origin: *" to the response (and configure the client ajax() call with dataType:"html")

在这种情况下,我能想到的唯一选择是在您控制的站点上设置代理。让该代理将请求/响应“代理”到“ http://wcidevapps.com”,但将 CORS 标头“Access-Control-Allow-Origin: *”添加到响应中(并配置客户端 ajax() 调用数据类型:“html”)

回答by Sahin Yanl?k

If you are using asp.net web service then you need to add this to webconfig file;

如果您使用的是 asp.net web 服务,那么您需要将其添加到 webconfig 文件中;

<system.webServer>
    <directoryBrowse enabled="true"/>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>
</system.webServer>