如何使用 jQuery 在不同端口上发送 AJAX 请求? [英] How do I send an AJAX request on a different port with jQuery?

查看:46
本文介绍了如何使用 jQuery 在不同端口上发送 AJAX 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我需要向端口 8080 发送 AJAX 请求,在那里运行守护程序.

I need to send an AJAX request to, for example, port 8080 where a daemon is running there.

推荐答案

您不能POST 信息跨域、子域或端口号.但是,如果您有权访问守护进程请求站点,则可以使用 JSONP.如果需要返回数据,则daemon 需要支持callback 查询参数,并以正确的格式返回.

You cannot POST information cross domain, subdomain, or port number. You can however use JSONP if you have access to both the daemon and the requesting site. If data needs to be returned, then the daemon needs to support a callback query parameter and return it properly formatted.

将信息传递给守护进程:

Pass the information to the daemon:

$.getJSON('http://domain.com:8080/url/here?callback=?', {
  key: 'value',
  otherKey: 'otherValue'
}, function(data){
     // Handles the callback when the data returns
});

现在只需确保您的守护进程处理 callback 参数.例如,如果 callback=mycallback 守护进程的返回(写入页面的唯一内容)应该如下所示:

Now just make sure your daemon handles the callback parameter. For instance, if callback=mycallback the return from the daemon (the only thing written to the page) should look like this:

对于键/值对:

mycallback( {'returnkey':'returnvalue', 'other':'data' });

对于数组:

mycallback( [1,2,3] );

如果您没有 JSONP 或类似的机制,则无法使用 jQuery 进行跨域通信.

If you do not have a JSONP or similar mechanism in place, you cannot communicate cross domain using jQuery.

这篇关于如何使用 jQuery 在不同端口上发送 AJAX 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆