0

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";
}
2
  • Are you making a cross domain request ? Commented Mar 21, 2012 at 13:36
  • You should console.log() more in the error CB. Commented Mar 21, 2012 at 13:39

2 Answers 2

1

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).')';
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the info, but i could not get this working. I have updated with my php code
@JPK but is the call on the same domain or on another?
i am making the ajax call from a local file on my desktop and will not be placed on the server
thanks for the information. I used your comments to do a little research and I managed to get it working a treat using http://www.webaddress/jsonp.php?method=getAllUsers&jsoncallback=?
1

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.

1 Comment

thanks for the feedback, i have updated my code to show my php file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.