2013-10-15 180 views
1

我已經積累了大約5或6個不同的教程,現在我仍然無法找到問題所在!使用JQuery從PHP獲取JSON響應

使用JQuery Mobile(phonegap)向PHP服務器發送和接收數據。我似乎無法讓JQuery腳本獲得響應。 PHP服務器上:

<?php 

// Set up associative array 
$data = array('success'=> true,'message'=>'Success message: worked!'); 

// JSON encode and send back to the server 
echo json_encode($data); 

?> 

jQuery函數(這是被稱爲):

<script type="text/javascript"> 
$(function() { 
    $('#inp').keyup(function() { 
     var postData = $('#inp').val(); 
     $.ajax({ 
      type: "POST", 
      dataType: "json", 
      data: postData, 
      beforeSend: function (x) { 
       if (x && x.overrideMimeType) { 
        x.overrideMimeType("application/json;charset=UTF-8"); 
       } 
      }, 
      url: 'removedmyurlfromhere', 
      success: function (data) { 
       // 'data' is a JSON object which we can access directly. 
       // Evaluate the data.success member and do something appropriate... 
       if (data.success == true) { 
        alert('result'); 
        $('.results').html(data.message); 
       } else { 
        $('.results').html(data.message); 
       } 
      } 
     }); 
    }); 
}); 
</script> 

很抱歉的格式,這一切又對形跨複製它。刪除了我的網址。

我知道該函數正在觸發,因爲如果我刪除了alert('here');並將其放在顯示的ajax調用上方。

有人知道爲什麼成功函數沒有調用?在任何瀏覽器上,div都不會顯示類結果。

謝謝!

+1

在進行ajax調用時,您在瀏覽器中得到了什麼響應?你真的得到200迴應?這是一個跨域請求嗎? –

+0

這只是一個數組,其關聯如下: {「success」:true,「message」:「成功消息:hooray!」} 添加tak3er建議的標頭我得到一個.json文件 文件存儲在本地,除了在網頁上外部存儲在服務器上的php文件之外。 – WelshJohn

+0

我懷疑這是一個跨域請求 –

回答

0

嘿,我有同樣的問題

首先,我用$ .getJSON等等等等....但沒有工作因爲某種原因...

但正常的Ajax調用工作..我的頁面會返回一個JSON輸出你..嘗試刪除「數據類型」

我使用JQUERY網站是下面我粘貼

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" } 
}) 
    .done(function(msg) { 
    alert("Data Saved: " + msg); 
    }); 
複製的代碼

下面是我如何使用,使用JSON輸出我得到工作的代碼...

$.ajax({ 
      type: "GET", 
      url: "json.php", 
      data: { searchword: q, projid:prid } 
     }) 
      .done(function(jsonResult) { 
      var str=''; 
     for(var i=0; i<jsonResult.length;i++) 
      { 
       //do something here.. with jsonResult."yournameof " 
      alert(jsonResult[i].id+jsonResult[i].boqitem); 
      } 
}); 
0

在PHP中,添加JSON內容類型標題:

header('Content-Type: application/json'); 

也嘗試收聽完整以及就看你得到任何響應返回:

$.ajax({ 

// .... 
complete: function (xhr, status) { 
    $('.results').append('Complete fired with status: ' + status + '<br />'); 
    $('.results').append('XHR: ' + (xhr ? xhr.responseText : '(undefined)')); 
} 
// ... 

}); 
+0

添加了上述標題並且沒有變化。沒有警報,沒有結果在div中。現在直接訪問頁面導致打開或保存.json文件。 – WelshJohn

+0

雖然添加標題是個好主意,但如果您在ajax選項中提供了dataType,則不是必需的。 –

+0

該文件打開,因爲你是瀏覽器不理解json mime類型。你有沒有看到我的編輯迴應完成調用:看看你是否得到迴應? – Stefan

0

保持在這裏記住同樣的域名起源,使用帶回調的jsonp一直對我有幫助,這意味着在您的php腳本中添加$_GET['callback']並將的json_encode包裝爲正確的格式。

http://api.jquery.com/jQuery.getJSON/

頁面上的最後演示是我已經找到了最好的例子。

0

貌似跨域請求。

嘗試通過改變:

dataType : "json" 

dataType : "jsonp" 
0

我知道這是非常晚。但它可以簡單地處理,就像

var postData = {'inp': $('#inp').val()}; 
$.ajax({ 
    type: "POST", 
    data: postData, 
    url: 'removedmyurlfromhere', // your url 
    success: function (response) { 
     // use exception handling for non json response 
     try { 
      response = $.parseJSON(response); 
      console.log(response); // or whatever you want do with that 
     } catch(e) {} 
    }, 
    error: function(jqXHR, textStatus, errorThrown) {}, 
    complete: function( jqXHR, textStatus) {} 
});