I'm trying to use AJAX to call a function in a PHP file.
The function basically takes data that is submitted in the AJAX call, makes a couple other requests to API's, and gets a JSON object from an external API.
I want to send that JSON object back to my page, to be acted on by javascript.
Here is my code:
function secureGet(sumNam){
$.ajax({
dataType: "json",
type: 'POST',
data: {sumNam: sumNam},
url: 'get_score.php',
success: function (json, state) {
console.log(state);
statsObject = json;
console.log(statsObject);
}
})
}
PHP:
<?php
require_once 'apikey.php';
if(isset($_POST['sumNam'])){
$name = $_POST['sumNam'];
secureProxy($name);
}
function secureProxy($summoner_name){
$url_one = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" . $summoner_name . "?api_key=" . $api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url_one);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result_one = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($result_one, true);
$summoner_id = $json_array[$summoner_name]['id'];
$url_two = "https://na.api.pvp.net/api/lol/na/v2.2/matchhistory/" . $summoner_id . "?rankedQueues=RANKED_SOLO_5x5,RANKED_TEAM_5x5&beginIndex=0&endIndex=10&api_key=" . $api_key;
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_URL, $url_two);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$result_two = curl_exec($ch2);
curl_close($ch2);
print_r($result_two);
}
?>
I think the call is successfully being made because in my console it says:
iGET XHR http://127.0.0.1/b2p/get_score.php [HTTP/1.1 200 OK 1551ms]
But the console isn't logging any information on the returned object.
Any reason as to why this might be happening?
Thanks!
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: api_key in C:\wamp\www\b2p\get_score.php on line <i>12</i>
...this goes on for a while
{"status": {"message": "Missing api key", "status_code": 401}}
But the API call works when I do it on its own.
dataType: "json",in your ajax call, but in php you are doingprint_r($result_two);which does not returnjson. Per the docs - api.jquery.com/jquery.ajax -... The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. .... You could tryecho json_encode($result_two);, depending on the value of$result_two.code:200tells you that you that ajax successfully found the page, but theSyntaxError: JSON.parse:tells you that your returned data was invalid json. Since the error message isunexpected character at line 1 column 1 of the JSON datait means that the returned data does not start with[or{. If$result_twois valid json already, you can just echo it directly.error_reportingenabled, they will echo the warnings out in any response that is sent right at the top. That will effectively invalidate any json you are sending back causing the 200 error, the error says your missing an API key somewhere, id start there, it will also give you the file name and line number