【发布时间】:2016-11-09 18:37:47
【问题描述】:
我有一个 Node.js 服务器在 localhost:3000 上侦听 作为一个黑盒,它输出我想要加载到本地网站的 UI。
用jQuery编写的消费服务是:
$('.my_selector').click(function(){
$.ajax({
url: ':3000/',
method: "POST",
success: function(data) {
$("#content").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
});
它不是直接访问页面,而是加载内容并将其附加到#content。
问题是代码不起作用。
编辑:
如果我将localhost:3000 设置为url,我会得到XMLHttpRequest Exception:
jquery.min.js:4 XMLHttpRequest cannot load localhost:3000/. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource
【问题讨论】:
-
那么有什么意义呢?希望
url: ':3000/'包含节点服务器的完整绝对url。如果你得到一个页面为什么你method: "POST"代码? -
-
正确。你需要使用JSONP请看这个解决方案Basic example of using .ajax() with JSONP
-
如果您的页面已经在 localhost:3000 上,请将您的 url 更改为 '/'。
-
@morels 不是针对不同域的 JSONP 吗?他们都在本地主机上。虽然这确实有效,但谢谢!
标签: jquery node.js vagrant jsonp