jQuery的JSON错误,当我想从蒸汽市场JSON [英] jquery json error, when i want to get json from steam market

查看:157
本文介绍了jQuery的JSON错误,当我想从蒸汽市场JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Ajax请求蒸汽的一个问题。 我想价格从蒸汽市场。

 函数jPrice(httpToJson){
    $ .getJSON(httpToJson,功能(数据){
        返回data.median_price;
    });
}
 

当我调用函数

<$p$p><$c$c>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');

我得到一个错误:

  

XMLHtt prequest无法加载<一个href="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" rel="nofollow">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.没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地'的http://lоcalhоst:63342,因此没有允许访问。

我尝试:

  • 设置PHP头访问控制 - 允许 - 产地为*
  • JS​​ONP

结果 - >同样的事情(错误)

也许有人知道解决这个问题呢?

解决方案

您将无法得到的结果在浏览器中通过直接针对steamcommunity.com做出Ajax请求,没有通过设置标题访问控制,允许-Origin为*,也不是通过发送JSONP请求。

对于这项工作,steamcommunity.com要么将 CORS 在响应头(你seing错误消息意味着它们不存在),或格式化输出为 JSON-P 。他们没有这样做。

这是一个浏览器限制,不允许来自不同来源的内容通过AJAX加载。你需要做的是推出一个中间件,所以有你的后端服务器,以使对steamcommunity.com的请求,并返回相同的反应,并进行Ajax调用对你的服务器。这将工作,后端发送请求,因为它不是一个浏览器的请求,响应将土地,比你的Ajax调用就能得到响应,以及​​因为它是针对同一个域


I have a problem with ajax request to Steam. 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. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://lоcalhоst:63342' is therefore not allowed access.

I try:

  • Set php header Access-Control-Allow-Origin to *
  • 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.

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. They didn't do either.

This is a browser restriction, do not allow the content from a different origin to be loaded via 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. 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

这篇关于jQuery的JSON错误,当我想从蒸汽市场JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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