不允许跨域ajax GET参数 [英] Cross domain ajax GET parameters not allowed

查看:230
本文介绍了不允许跨域ajax GET参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript从API获取数据,但是我在请求中遇到了错误.

I'm trying to get data from an API with javascript but i'm getting an error on the request.

$.ajax({
    dataType: "jsonp",
    url: "https://www.bitstamp.net/api/ticker/",
    type: "GET",
    succes: myfunction
});

结果:

{"error": "GET parameters not allowed for this request."}

我使用Jsonp是因为它是另一个领域. 为什么我无法通过Jquery获取数据? 如果我只是浏览到链接,那么可以看到Json.

I use Jsonp because its another domain. Why can't I get the data with Jquery? If I just browse to the link I can see the Json.

推荐答案

我刚刚尝试从使用AJAX提供的网址中获取数据.服务器未使用$.ajax返回任何数据,这清楚地表明服务器不支持跨域请求.这就是为什么我问您是否有权访问代码的原因,因为您必须手动访问指定您是否要API支持跨域请求.

I just tried getting data from the url you provided using AJAX. The server did not return any data using the $.ajax and this clearly shows that the server does not support cross domain requests. That is why I asked you if you had access to code because you have to manually specify if you want API to support cross domain requests.

一种解决方法是使用某种服务器端语言来访问此API.我曾经遇到过类似的问题,并且使用 PHP CURL 访问API.然后,PHP代码将数据提供给JQuery以在前端使用.因此,您可以编写中继代码来解决此问题.

One way around to this is using some server side language to access this API. I once had similar problem and the used PHP CURL to access the API. The php code then served data to JQuery to be used on frontend. So you can write relay code to solve this problem.

这篇关于不允许跨域ajax GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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