简体   繁体   English

使用jQuery同步交叉子域POST请求

[英]Synchronous cross sub-domain POST request with jQuery

I'm trying to do a cross domain POST request and have hit a wall (or two). 我正在尝试执行跨域POST请求并且已经打了一个墙(或两个)。

I can't put a proxy page on the server - so that is not an option. 我无法在服务器上放置代理页面 - 因此这不是一个选项。

I have researched getJSON, which works great except that I need to POST not GET. 我研究了getJSON,除了我需要POST不GET之外,它工作得很好。

Is it possible to do this? 是否有可能做到这一点? If it is not , can someone explain to me how getJSON works and why I cannot make a POST alternative. 如果不是 ,有人可以向我解释getJSON如何工作以及为什么我不能做出替代方案。

You CANNOT make a cross-domain request (GET / POST / etc.) with an XMLHttpRequest (aka AJAX). 不能使用XMLHttpRequest(aka AJAX)发出跨域请求(GET / POST /等)。

What you can do, when the server supports it, is make a JSONP request. 当服务器支持它时,您可以做的是发出JSONP请求。 A JSONP request works as follows: JSONP请求的工作方式如下:

  • jQuery creates a globally accessible function out of the callback function you provide as an argument jQuery使用您提供的回调函数创建一个全局可访问的函数作为参数
  • Instead of using XMLHttpRequest (AJAX) to make the HTTP request, jQuery dynamically inserts a SCRIPT tag into the DOM 而不是使用XMLHttpRequest(AJAX)来发出HTTP请求,jQuery动态地将SCRIPT标记插入到DOM中
  • The SRC of the script tag is the request URL to which you are trying to communicate 脚本标记的SRC是您尝试与之通信的请求URL
  • jQuery adds a callback param to the query string like so: example.com/someurl.js?callback=someDynamicallyGeneratedMethodName jQuery向查询字符串添加一个回调参数,如下所示:example.com/someurl.js?callback=someDynamicallyGeneratedMethodName
  • It is then up to the SERVER to return JavaScript that your client can use by passing the JSON result as an argument to someDynamicallyGeneratedMethodName 然后由SERVER通过将JSON结果作为参数传递给someDynamicallyGeneratedMethodName来返回客户端可以使用的JavaScript

If you have no control of the server that you are posting to, then you are out of luck, JSONP won't do you much good. 如果您无法控制要发布的服务器,那么您运气不佳,JSONP对您不会有太大帮助。 Whatever the server returns will be in a SCRIPT tag, and will most likely throw an error if it isn't formatted correctly. 无论服务器返回什么都将在SCRIPT标记中,并且如果格式不正确,很可能会抛出错误。

For more info on this, I suggest you look at the base $.ajax function instead of the shortcuts. 有关这方面的更多信息,我建议您查看基本的$ .ajax函数而不是快捷方式。 (In the jQuery documentation under Ajax. Sorry I can't post more links) (在Ajax下的jQuery文档中。抱歉,我无法发布更多链接)

Again, if you don't have control of the server you are posting to, you might want to look into a proxy if possible. 同样,如果您无法控制要发布的服务器,则可能需要查看代理。 Otherwise, an IFRAME may be your only other option. 否则,IFRAME可能是您唯一的其他选择。 There is also a method to accomplish this with a SWF (flash) object. 还有一种方法可以使用SWF(flash)对象来实现此目的。 I have tried neither, but they are workarounds to the limitations of the XMLHttpRequest object. 我既没有尝试过,但它们是XMLHttpRequest对象限制的解决方法。

Hope I could help! 希望我能帮忙!

You can do a post, but what you want is a JSONP request to get around the cross domain issues. 你可以做一个帖子,但你想要的是一个JSONP请求来解决跨域问题。 Essentially you provide a callback function and the request comes back as script content and your callback gets called with the JSON data from the request. 本质上,您提供了一个回调函数,请求以脚本内容的形式返回,并使用请求中的JSON数据调用您的回调。 Your server side script will need to provide the data back as a function call using the callback function wrapped around the JSON object. 您的服务器端脚本需要使用围绕JSON对象的回调函数将数据作为函数调用提供。

See the documentation on the post function. 请参阅post功能的文档。

$.post( '/example.com/controller/action?callback=?',
        { param: "data" }, 
        function(data) {
             ...do something with the data...
        }, 'jsonp' );

ASP.NET MVC action for this: ASP.NET MVC对此的操作:

[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Action( string param, string callback )
{
     var jsonData = ...do something and construct some data in JSON...

     return Content( callback + "(" + jsonData + ");" );
}

If you want to do Cross Domain POST then the easiest solution is the one provided here by Matteo. 如果你想要做的跨域POST那么最简单的解决方案是提供一个在这里利玛窦。 It worked great for me 它对我很有用

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

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