jQuery - 跨域请求不起作用

时间:2015-12-23 17:22:39

标签: javascript jquery html ajax

我希望通过$.ajax获取一些数据。我只需要响应(响应是JSON)。 这就是我现在所拥有的:

$.ajax({
type: 'GET',
url: "example.com",
processData: true,
data: {},
dataType: "json",
success: function (data) {
  alert(data);
} 
});

...但是我总是得到一个错误的原因,所以我把代码改为

... dataType: "jsonp"

...但现在我收到了错误

  

Uncaught SyntaxError:意外的令牌:

我做错了什么?我无法更改标题或.htaccess内容,因为我无法访问目标服务器。

谢谢。

1 个答案:

答案 0 :(得分:0)

请使用完整的网址,而不是" example.com"喜欢 " http://www.domainname.com/example.com

$.ajax({
type: 'GET',
url: "example.com",
processData: true,
data: {},
dataType: "json",
success: function (data) {
 alert(data);
} 
});