1

I have problem receiving data from api with all other links it was ok but that one is so hard .. So here is the code

$.ajax({
url: 'proxy.php?url=https://na.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/NA1/26667724?api_key=xxxx',
dataType:"json",
success: function() {
alert("Success");
},
error: function() {
console.log("Error")
}
});

And this is the php code that i am using .

    <?php
header("Content-Type: text/javascript; charset=utf-8");
if (!isset($_GET['url'])) {
   die(); 
}
$url = urldecode($_GET['url']);
$url = 'https://' . str_replace('https://', '', $url); 
echo file_get_contents($url);
?>

On the console log is displayed --->XMLHttpRequest cannot load file:///D:/Install/xampp/htdocs/allInOne/proxy.php?url=https://na.api.pvp.n…pectatorGameInfo/NA1/26667724?api_key=xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.send @ jquery-1.11.3.js:9664jQuery.extend.ajax @ jquery-1.11.3.js:9215jQuery.(anonymous function) @ jquery-1.11.3.js:9361jQuery.extend.getJSON @ jquery-1.11.3.js:9344renderInfo @ render.js:89onclick @ index.html:15 render.js:85 Error

QQstuf
  • 15
  • 1
  • 5
  • Have you checked manually, what `proxy.php` returns? my guess is, that there is some kind of html error message there. – Sirko Jan 09 '16 at 12:19

3 Answers3

2

Replace

header("Content-Type: text/javascript; charset=utf-8")

with

header("Content-Type: text/javascript; charset=utf-8");
//....................................................^You miss this ';'
Kausha Mehta
  • 2,828
  • 2
  • 20
  • 31
2

Looks like the error is in the proxy.php file, it cannot use file_get_contents because the url returns 404.

and the php echoes

<br />
<b>Warning</b>:  file_get_contents(https://euw.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/EUW1/19496550?api_key=xxxx): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in <b>C:\xampp\htdocs\test\proxy.php</b> on line <b>8</b><br />

and when the javascript tries to read the response it fails.

Maybe this url is wrong? https://euw.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/EUW1/19496550?api_keyxxx EDIT

remove

headers:{"Access-Control-Allow-Origin:": "*",
        'Access-Control-Allow-Headers':"X-Requested-With",},
crossDomain: true,

and change

dataType:"jsonp",

to

dataType:"json",

crossdomain requests only needed in javascript, when you are requesting data from a url with Php this rule wont be needed

EDIT2 The porblem was loading the html file directly be clicking, thus making it give the error for the cross origin policy. Fixed by address the file from xamp

QQstuf
  • 15
  • 1
  • 5
BinaryGhost
  • 801
  • 4
  • 9
  • sec i will provide new url – QQstuf Jan 09 '16 at 12:37
  • donenew url same error – QQstuf Jan 09 '16 at 12:43
  • the proxy.php is in the same domain as the ajax file? if it is then you should change dataType:"jsonp" to dataType:"json", And remove headers:{"Access-Control-Allow-Origin:": "*", 'Access-Control-Allow-Headers':"X-Requested-With",}, crossDomain: true, – BinaryGhost Jan 09 '16 at 12:54
  • @ BinaryGhost now i get thiss --->Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. – QQstuf Jan 09 '16 at 13:01
  • Yep, you dont need cross domain requests when your are requesting the data with php. Is it working now? – BinaryGhost Jan 09 '16 at 13:05
  • the log still displays error :X... – QQstuf Jan 09 '16 at 13:10
  • How are you trying to load the html file? directly by clicking on it? or by using xamp. For example http://localhost/allInOne/HTMLFILENAME.html You wont be able to proccess the request if you just double click on the html... – BinaryGhost Jan 09 '16 at 13:38
  • It workt TY very mutch you are the best in the world ^_^_^_^_^_^ – QQstuf Jan 09 '16 at 13:39
  • No problem, glad to help :) – BinaryGhost Jan 09 '16 at 13:44
-1

Your Header line is

header("Content-Type: text/javascript; charset=utf-8")

My header line is

header("Content-Type: text/javascript; charset=utf-8");
Ankit vadariya
  • 1,253
  • 13
  • 14