简体   繁体   English

如何使用Ajax和jsonp进行跨域查询?

[英]How can i do cross domain query using ajax & jsonp?

I send ajax to url , but i get error. 我将ajax发送到url,但出现错误。 Here is my code: 这是我的代码:

$.ajax({
    url: "http://webrates.truefx.com/rates/connect.html?q=ozrates&c=EUR/USD&f=csv&s=n",
    dataType : 'jsonp',
    success: function (data) {
        alert(data);
    }
})

Maybe i doing something wrong? 也许我做错了什么?

In order to access data using JSONP, the server you are requesting the data from must format its response as JSONP. 为了使用JSONP访问数据,请求数据的服务器必须将其响应格式设置为JSONP。

You are getting an error because the response is not JSONP (it is CSV, which you asked for explicitly when you put t=csv in the query string). 您收到错误消息是因为响应不是 JSONP(它是CSV,是在将t=csv放入查询字符串中时明确要求的)。

Either: 或者:

  • Use a different method to get the data 使用其他方法获取数据
  • Change the server so it responds with JSONP formatted data 更改服务器,使其以JSONP格式的数据响应

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

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