简体   繁体   English

jQuery json错误,当我想从Steam市场获取json时

[英]jquery json error, when i want to get json from steam market


I have a problem with ajax request to Steam. 我对Steam的ajax请求有问题。 I want to get price from steam market. 我想从蒸汽市场上获取价格。

function jPrice(httpToJson) {
    $.getJSON(httpToJson, function(data) {
        return data.median_price;
    });
}

When I call function 当我调用函数

jPrice('http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29');

I get an error: 我收到一个错误:

XMLHttpRequest cannot load http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29 . XMLHttpRequest无法加载http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'http://lоcalhоst:63342' is therefore not allowed access. 因此,不允许访问来源“ http://lоcalhоst:63342”。

I try: 我尝试:

  • Set php header Access-Control-Allow-Origin to * 将php标头Access-Control-Allow-Origin设置为*
  • JSONP JSONP

RESULT -> The same thing (error)! 结果->同一件事(错误)!

Maybe someone knows a solution to this problem? 也许有人知道该问题的解决方案?

You won't be able to get the results in your browser via ajax request made directly against steamcommunity.com, neither by setting the header Access-Control-Allow-Origin to *, nor by sending a JSONP request. 通过将标头Access-Control-Allow-Origin设置为*或发送JSONP请求,都将无法通过直接针对steamcommunity.com发出的ajax请求在浏览器中获得结果。

For this to work, steamcommunity.com should either add CORS headers in the response (the error message you're seing means that they are not there), or format the output to be JSON-P . 为此,steamcommunity.com应该在响应中添加CORS标头(您所看到的错误消息意味着它们不存在),或将输出格式化为JSON-P They didn't do either. 他们也没有。

This is a browser restriction, do not allow the content from a different origin to be loaded via ajax. 这是浏览器的限制,不允许通过ajax加载其他来源的内容。 What you need to do is introduce a middle-ware, so have your back-end server to make a request against steamcommunity.com and return the same response, and make the ajax call against you're server. 您需要做的是引入一个中间件,因此让您的后端服务器对steamcommunity.com发出请求并返回相同的响应,然后对您的服务器进行ajax调用。 This will work, your back-end is sending the request, and as it is not a browser request, the response will land, than your ajax call will be able to get the response as well since it is issued against the same domain 这将起作用,您的后端正在发送请求,并且由于它不是浏览器请求,因此响应将到达,因为您的ajax调用是针对同一域发出的,所以它也将获得响应

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

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