将JSON从服务器端(.NET)传递到客户端(jQuery) [英] Passing JSON from server-side (.NET) to client-side (jQuery)
问题描述
我正在尝试从下面的URL加载JSON:
I'm trying to load in JSON from the URL below :
https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001090170/timeline?gameHash = a23ccbe7928a63a3
我的代码如下:
var baseUrl = "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885";
var json = $.getJSON(baseUrl, function(data){
});
$("#output").text(JSON.stringify(json));
我的输出显示为:
{"readyState":1}
Jsfiddle:
https://jsfiddle.net/6Lwjpjmo/
编辑:显然由于以下两个原因,无法从该URL检索JSON:
EDIT : Apparently it is not possible to retrieve the JSON from that URL because of 2 reasons:
-
Access-Control-Allow-Origin:null
- 通过Proxy Requeste调用JSON,以将JSON加载到正文中.
我已经可以通过.NET检索JSON服务器端,但是需要将某些变量传递给客户端以在jQuery中进行操作.我该怎么办?
I already can retrieve the JSON server-side via .NET but need to pass on certain variables to the client-side to manipulate in jQuery. How do I go about this?
推荐答案
您不需要那么麻烦,服务器可以接受JSONP
.只需将callback=?
参数添加到您的网址中,它就可以正常工作.
You do not need to do so difficult, the server does accept JSONP
. Just add the callback=?
parameter to your url and it works fine.
baseUrl = "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885&callback=?";
"JSON"回来的话畸形了
The "JSON" that you get back is malformed however
SyntaxError: Unexpected token ':'. Parse error. (anonymous function)timeline:1
编辑
服务器似乎也不返回JSONP,使用代理服务可以使其正常工作:
EDIT
It appears that the server does not return JSONP either, using a proxy service you can make it work:
var proxyUrl = 'https://jsonp.afeld.me/';
var serviceUrl = "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885";
var url = proxyUrl + '?url=' + encodeURIComponent(serviceUrl) + '&callback=?';
这篇关于将JSON从服务器端(.NET)传递到客户端(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!