So, I’m requesting a file named uuid.php
, and when I request it via JQUERY AJAX, it returns the contents of that php
file. I’ve been on multiple other Stack Overflow posts describing the error, and I couldn’t get a good answer for my problem.
JQUERY request:
$.ajax({
type: "POST",
url: "uuid.php",
datatype: "json",
data: {"kahoot":"2020"},
success: function(data) {
console.log(data)
}
});
uuid.php
<?php
$query=$_POST['kahoot']
$url = 'https://create.kahoot.it/rest/kahoots/?query='.$query;
$contents = file_get_contents($url);
echo json_encode(array(data=>$contents));
?>
I’ve tried the PHP
code outside of this project, and it works fine. There were no errors, but when I put it into the project it doesn’t work. Is this an error with AJAX?
3
Answers
You forgot to add ; in this line:
It seems that visiting the URL directly (https://create.kahoot.it/rest/kahoots/?query=kahoot) returns a JSON response so the json_encode may be more that needed.
Does the output from this look any better:
Your JS file is ok,
I have changed your PHP code a bit:
First of all you have forget to add
;
on the first row.And I’ve updated the last row to this:
echo json_encode(array($contents));
instand ofecho json_encode(array(data=>$contents));
That should do.