TaylorM
TaylorM

Reputation: 109

Passing JSON from server-side (.NET) to client-side (jQuery)

I'm trying to load in JSON from the URL below :

https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001090170/timeline?gameHash=a23ccbe7928a63a3

My code looks like this :

   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));

And my output reads this :

{"readyState":1}

Jsfiddle:

https://jsfiddle.net/6Lwjpjmo/

EDIT : Apparently it is not possible to retrieve the JSON from that URL because of 2 reasons:

  1. Access-Control-Allow-Origin:null
  2. JSON is called via Proxy Requeste to load the JSON in the body.

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?

Upvotes: 1

Views: 378

Answers (2)

Niki van Stein
Niki van Stein

Reputation: 10724

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=?";

The "JSON" that you get back is malformed however

SyntaxError: Unexpected token ':'. Parse error. (anonymous function)timeline:1

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=?';

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

It is an asynchronous function. Moreover, you are receiving only the XHR object. So, put it inside the function:

var baseUrl = "https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885";

$.getJSON(baseUrl, function(data) {
  json = JSON.parse(data);
  $("#output").text(JSON.stringify(json));
});

This should work.

You need to use a Server Side Script like Proxy PHP file, that reads the content and executes it correctly.

Proxy.php:

<?php
    header("Content-type: application/json");
    die(file_get_contents($_GET["url"]));
?>

And call it like this:

url: "proxy.php?url=https://acs.leagueoflegends.com/v1/stats/game/TRLH3/1001440043/timeline?gameHash=4725b07311676885"

Upvotes: 2

Related Questions