用jQuery同步跨子域POST请求

发布于 2024-08-07 10:58:42 字数 185 浏览 3 评论 0原文

我正在尝试执行跨域 POST 请求,但遇到了(或两处)困难。

我无法在服务器上放置代理页面 - 所以这不是一个选项。

我研究了 getJSON,它效果很好,只是我需要 POST 而不是 GET。

可以这样做吗?如果不是,有人可以向我解释 getJSON 是如何工作的以及为什么我不能做出 POST 替代方案。

I'm trying to do a cross domain POST request and have hit a wall (or two).

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-08-14 10:58:42

不能使用 XMLHttpRequest(又名阿贾克斯)。

当服务器支持时,您可以做的是发出 JSONP 请求。 JSONP 请求的工作方式如下:

  • jQuery 根据您作为参数提供的回调函数创建一个全局可访问的函数
  • jQuery 不是使用 XMLHttpRequest (AJAX) 发出 HTTP 请求,而是动态地将 SCRIPT 标记插入到 DOM 中
  • 脚本的 SRC标签是您尝试与之通信的请求 URL
  • jQuery 将回调参数添加到查询字符串中,如下所示: example.com/someurl.js?callback=someDynamicallyGeneratedMethodName
  • 然后由服务器返回您的客户端可以使用的 JavaScript将 JSON 结果作为参数传递给 someDynamicallyGenerateMethodName

如果您无法控制要发布到的服务器,那么您就不走运了,JSONP 不会给您带来太大好处。服务器返回的任何内容都将位于 SCRIPT 标记中,如果格式不正确,很可能会抛出错误。

有关这方面的更多信息,我建议您查看基本 $.ajax 函数而不是快捷方式。 (在 Ajax 下的 jQuery 文档中。抱歉,我无法发布更多链接)

同样,如果您无法控制要发布到的服务器,则可能需要查看代理(如果可能)。否则,IFRAME 可能是您唯一的选择。还有一种方法可以使用 SWF (flash) 对象来实现此目的。我没有尝试过,但它们是解决 XMLHttpRequest 对象限制的方法。

希望我能帮忙!

You CANNOT make a cross-domain request (GET / POST / etc.) with an XMLHttpRequest (aka AJAX).

What you can do, when the server supports it, is make a JSONP request. A JSONP request works as follows:

  • jQuery creates a globally accessible function out of the callback function you provide as an argument
  • Instead of using XMLHttpRequest (AJAX) to make the HTTP request, jQuery dynamically inserts a SCRIPT tag into the DOM
  • The SRC of the script tag is the request URL to which you are trying to communicate
  • jQuery adds a callback param to the query string like so: 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

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. Whatever the server returns will be in a SCRIPT tag, and will most likely throw an error if it isn't formatted correctly.

For more info on this, I suggest you look at the base $.ajax function instead of the shortcuts. (In the jQuery documentation under Ajax. Sorry I can't post more links)

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. There is also a method to accomplish this with a SWF (flash) object. I have tried neither, but they are workarounds to the limitations of the XMLHttpRequest object.

Hope I could help!

枯叶蝶 2024-08-14 10:58:42

您可以发布帖子,但您想要的是 JSONP 请求来解决跨域问题。本质上,您提供一个回调函数,请求作为脚本内容返回,并且使用请求中的 JSON 数据调用您的回调。您的服务器端脚本需要使用围绕 JSON 对象的回调函数将数据作为函数调用提供回来。

请参阅有关 post 函数的文档。

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

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 + ");" );
}

You can do a post, but what you want is a JSONP request to get around the cross domain issues. 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. Your server side script will need to provide the data back as a function call using the callback function wrapped around the JSON object.

See the documentation on the post function.

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

ASP.NET MVC action for this:

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

     return Content( callback + "(" + jsonData + ");" );
}
唯憾梦倾城 2024-08-14 10:58:42

如果您想进行跨域 POST,那么最简单的解决方案是此处提供的解决方案马特奥.
这对我很有用

If you want to do Cross Domain POST then the easiest solution is the one provided here by Matteo.
It worked great for me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文