简体   繁体   中英

JQUERY AJAX call returning PHP page contents

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?

You forgot to add; in this line:

$query=$_POST['kahoot']

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:

<?php
$query=$_POST['kahoot']
$url = 'https://create.kahoot.it/rest/kahoots/?query='.$query;
echo file_get_contents($url);
?>

Your JS file is ok, I have changed your PHP code a bit:

<?php
    $query=$_POST['kahoot'];
    $url = 'https://create.kahoot.it/rest/kahoots/?query='.$query;
    $contents = file_get_contents($url);
    echo json_encode(array($contents));
?>

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 of echo json_encode(array(data=>$contents));

That should do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM