简体   繁体   English

是否有可能使用JQuery“a”跨域数据.ajax()

[英]Is it possible to 'PUT' data cross-domain with JQuery .ajax()

We try to put some data to our server, the serverside should be okay because we tested already with DEV HTTP CLIENT Chrome extension. 我们尝试将一些数据放到我们的服务器上,服务器端应该没问题,因为我们已经使用DEV HTTP CLIENT Chrome扩展进行了测试。 This is our code, I think it should work but I don't know how I could fix the cross domain error. 这是我们的代码,我认为它应该可以工作,但我不知道如何修复跨域错误。

$.ajax({
        type: "PUT",
        url: '...../add?callback=JSONPCallback',
        contentType: "application/jsonp; charset=utf-8",
        data: JSON.stringify({
            "name": "Test",
        }),
        jsonpCallback: "JSONPCallback",
        crossDomain: true,
        success: function (result) {
            console.log('Success!');
        },
        error: function (a, b, c) {
            console.error(a + " " + b + " " + c);
        },
        fail: function (e) {
            alert('fail');
        }
    });
}

In the GET requests we used 'dataType: 'jsonp' ', but this is not working for PUT . 在GET请求中,我们使用'dataType:'jsonp'',但这不适用于PUT。 Is there any possibility to PUT a json object and GET a jsonp object back in the success method? 是否有可能在成功方法中输出一个json对象并获取一个jsonp对象?

JSONP is a GET operation (specifically, one created via a script tag). JSONP是一个GET操作(具体来说,是通过script标记创建的)。

You can do cross-domain PUT , just not in the JSONP way. 可以执行跨域PUT ,而不是以JSONP方式。 Your target server has to allow your origin via Cross Origin Resource Sharing (and your browser has to support CORS with XMLHttpRequest , not the IE8 and IE9 XDomainRequest object — although if you need IE8 and IE9 support, if you look around you can find a plugin for jQuery that makes jQuery use XDomainRequest on those browsers). 您的目标服务器必须通过跨源资源共享允许您的源(并且您的浏览器必须支持带有XMLHttpRequest CORS ,而不是IE8和IE9 XDomainRequest对象 - 尽管如果您需要IE8和IE9支持,如果您环顾四周,您可以找到一个插件对于使jQuery在这些浏览器上使用XDomainRequest jQuery)。

If you are using modern browser (ie Firefox and chrome, safari), you can use HTML5 API CORS for doing this. 如果您使用的是现代浏览器(即Firefox和chrome,safari),则可以使用HTML5 API CORS执行此操作。 Here is the link for Using cors 这是使用cors的链接

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

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