JPK
JPK

Reputation: 491

Ajax call not returning data from php file

i am having a little trouble with the following ajax call;

function question()  {
//ajax call to fetch data from database
var course = "St.Andrews";
var dataString = "course=" + course;

$.ajax({
     type:  "GET",
     url:   "http://www.webaddress/fetch.php",
     datatype: "json",
     data: course,

     success: function(datas){
     console.log(datas);
     },
     error: function(XMLHttpRequest, textStatus, errorThrown){
     console.log("error:" + XMLHttpRequest.responsetext);
     }
     });
}

For some reason, i cannot get the fetched results to display. My php file that returns the results works fine if i navigate to it from the browser and i get the returned results in the valid format.

When i check the console log, i can see the params are correct and i get error:undefined. Can anyone provide as to what i'm doing wrong, thanks.

Here is my php script;

<?php
//include databse details
require_once 'login.php';

//connect to database or return error
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("unable to connect to MYSQL:" . mysql_error());

//select database or return error
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());

if (isset($_GET['course'])) {

    $course = $_GET['course'];

    //set the character code
    mysql_query('SET CHARACTER SET utf8');

    //make the query
    $query = "SELECT * FROM questions WHERE course = '" . $course . "' ";
    $result = mysql_query($query) or die (mysql_error());

    if ($result !== false && mysql_num_rows($result) > 0)   {

        $row = mysql_fetch_array($result, MYSQL_ASSOC);

        $question = $row['question'];
        $answer = $row['answer'];
        $incorrect = $row['incorrectAnswer'];
        $difficulty = $row['difficulty'];

        //Json row
        $json = array ("question" => $question, "answer" => $answer, "incorrect" => $incorrect, "difficulty" => $difficulty);
    } else {
        //catch any errors
        $json = array("error" => "Mysql query error");
    }

    //set header for json data
    header("Content-Type: application/json", true);
    //return Json
    echo json_encode($json);
} else {
    echo "Needs course to advance dingbat";
}

Upvotes: 0

Views: 1185

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

The most probable reason is that you are calling that url from a different domain and that's not possible using plain json for security reasons, you should use jsonp.

To correct that: js

var course = "St.Andrews";
var dataString = {course: course, callback : "?"};

$.ajax({
     type:  "GET",
     url:   "http://www.webaddress/fetch.php",
     datatype: "jsonp",
     data: course,

     success: function(datas){
     console.log(datas);
     },
     error: function(XMLHttpRequest, textStatus, errorThrown){
     console.log("error:" + XMLHttpRequest.responsetext);
     }
     });
}

php

<?php header('content-type: application/javascript; charset=utf-8');

$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

echo $_GET['callback'] . '('.json_encode($data).')';

Upvotes: 1

hvgotcodes
hvgotcodes

Reputation: 120308

I see two possibilities:

1) Your php script is not returning valid json. You specify json as the datatype parameter, be sure you are actually returning json.

2) You are violating the same origin policy. Your url is http://www.webadddress/...unless the browser loaded the script from that some url, it won't be able to access that url with an xhr.

Upvotes: 1

Related Questions