skip to Main Content

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


  1. You forgot to add ; in this line:

    $query=$_POST['kahoot']
    
    Login or Signup to reply.
  2. 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);
    ?>
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search