2012-03-21 66 views
0

我對以下ajax調用有點麻煩;Ajax調用不返回來自php文件的數據

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); 
    } 
    }); 
} 

出於某種原因,我無法獲取要顯示的提取結果。我的php文件返回的結果工作正常,如果我從瀏覽器導航到它,我得到有效格式返回的結果。

當我檢查控制檯日誌,我可以看到參數是正確的,我得到的錯誤:未定義。 任何人都可以提供什麼我做錯了,謝謝。

這是我的PHP腳本;

<?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"; 
} 
+0

你做跨域請求? – Shyju 2012-03-21 13:36:07

+0

你應該在錯誤CB中加入更多的console.log()。 – Shikiryu 2012-03-21 13:39:56

回答

1

最有可能的原因是,您從不同的域調用該URL,出於安全原因使用普通json是不可能的,您應該使用jsonp。

要更正: 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).')'; 
+0

感謝您的信息,但我無法得到這個工作。我更新了我的php代碼 – JPK 2012-03-21 13:58:03

+0

@JPK,但是是在同一個域還是另一個域上的調用? – 2012-03-21 14:02:50

+0

我正在從我的桌面上的本地文件進行ajax調用,並且不會將其放置在服務器 – JPK 2012-03-21 14:05:49

1

我看到兩種可能性:

1)你的PHP腳本沒有返回有效的JSON。你指定json作爲數據類型參數,確保你實際上返回了json。

2)您違反了同一來源政策。您的網址是http://www.webadddress/ ...除非瀏覽器從某個網址加載了該腳本,否則它將無法通過xhr訪問該網址。

+0

注意這適用於www.domain.com和domain.com。 – 2012-03-21 13:38:19

+0

Topener,是的,我剛剛複製了他在他的例子中的網址... – hvgotcodes 2012-03-21 13:39:17

+0

感謝您的反饋,我已更新我的代碼,以顯示我的PHP文件 – JPK 2012-03-21 13:45:00