简体   繁体   English

带有jQuery 1.5.1的跨域ajax

[英]Crossdomain ajax with jquery 1.5.1

I have used ajax with jquery 1.4.2 on my domain, but I need to get a page from another domain. 我在我的域上将ajax与jquery 1.4.2一起使用,但是我需要从另一个域获取页面。 The following code works with jquery 1.4.2 but I want to use 1.5.1: 以下代码适用于jquery 1.4.2,但我想使用1.5.1:

$.ajax({
    type: "POST",
    dataType: "html",
    crossDomain: true,
    url: "http://www.domain.com/page.aspx",
    error: function() {
        alert("error");
    },
    success: function(msg){
        alert(msg );
    }
});

For some reason I get an error, but when I change to jquery 1.4.2 it works. 由于某种原因,我得到一个错误,但是当我更改为jQuery 1.4.2时,它可以工作。

what am I doing wrong? 我究竟做错了什么?

You need to add the following header in the response (need control over both domains) 您需要在响应中添加以下标头(需要同时控制两个域)

Header set Access-Control-Allow-Origin "http://your-domain.com" 标头集Access-Control-Allow-Origin "http://your-domain.com"

In your Javascript code you need to add 在您的Javascript代码中,您需要添加

jQuery.support.cors = true;

It works at least with 1.6.2, but only in Firefox (and Chorme I think), IExplorer don't support CORS standard... 它至少与1.6.2兼容,但仅在Firefox(和我认为的Chorme)中,IExplorer不支持CORS标准...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM