2016-07-07 48 views
0

我試圖發送一個JQuery請求到我正在做的API(第一次,學習!),但我的PHP代碼報告JSON格式不正確。當從JQuery發送到PHP時格式不正確JSON

如果我在PHP中創建一個JSON數組,並通過它可以正常工作,但如果我嘗試通過JQuery請求它總是說格式不正確。

我卡住了!

JavaScript端看起來像這樣...

jsonrequest = "{request: 'getJobs', token : 'eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2'}"; 
    $.ajax({ 
    url: 'api.php', 
    async: true, 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    data: jsonrequest, 


     success: function (result) { 
      console.log(result); 
     } 
}); 

和PHP看起來像這樣

$data  = file_get_contents("php://input"); 

if(isJson($data)) { 

     // never passes the isJSON validation. 

     $json  = json_decode($data,true); 
     $request = sanitize($json['request']); 
     } 


function isJSON($string) 
{ 
    // decode the JSON data 
    $result = json_decode($string); 

    // switch and check possible JSON errors 
    switch (json_last_error()) { 
     case JSON_ERROR_NONE: 
      $error = ''; // JSON is valid // No error has occurred 
      break; 
     case JSON_ERROR_DEPTH: 
      $error = 'The maximum stack depth has been exceeded.'; 
      break; 
     case JSON_ERROR_STATE_MISMATCH: 
      $error = 'Invalid or malformed JSON.'; 
      break; 
     case JSON_ERROR_CTRL_CHAR: 
      $error = 'Control character error, possibly incorrectly encoded.'; 
      break; 
     case JSON_ERROR_SYNTAX: 
      $error = 'Syntax error, malformed JSON.'; 
      break; 
     // PHP >= 5.3.3 
     case JSON_ERROR_UTF8: 
      $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; 
      break; 
     // PHP >= 5.5.0 
     case JSON_ERROR_RECURSION: 
      $error = 'One or more recursive references in the value to be encoded.'; 
      break; 
     // PHP >= 5.5.0 
     case JSON_ERROR_INF_OR_NAN: 
      $error = 'One or more NAN or INF values in the value to be encoded.'; 
      break; 
     case JSON_ERROR_UNSUPPORTED_TYPE: 
      $error = 'A value of a type that cannot be encoded was given.'; 
      break; 
     default: 
      $error = 'Unknown JSON error occured.'; 
      break; 
    } 

    if ($error !== '') { 
     // throw the Exception or exit // or whatever :) 
     $output = array('status' => "error",'message' => $error); 
     echo json_encode($output); 
     exit; 
    } 

    // everything is OK 
    return $result; 
} 
+0

JSON在對象鍵上也需要引號。 –

回答

2

嘗試

data: {request: 'getJobs', token: 'eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2'} 

的周圍沒有引號。 jQuery應該爲你處理。

如果仍然失敗,可以在開發人員工具中打開它,查看ajax請求並查看數據實際上看起來像什麼以獲取想法。

+0

這反映了我要完全迴應的內容。這可能是html編碼的輸入字符串。 –

+0

我已經把報價放在了上面,但沒有什麼區別,我沒有意識到開發者工具會告訴我這個外向的請求。很酷的功能...在網絡部分是這個'api.php?request = getJobs&token = eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2'這是否意味着它作爲一個標準的GET類型的請求而不是編碼的JSON? –

+0

@CraigArmitage是的,這是一個GET請求,您可以使用$ .post()或者包含'method:「POST」'作爲$ .ajax()中的一個屬性。 http://api.jquery.com/jquery.ajax/ – neilsimp1

1

您的代碼

jsonrequest = "{request: 'getJobs', token : 'eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2'}"; 

要定義jsonrequest作爲一個字符串不是一個對象。

嘗試以下兩種情況之一(注意鍵名在引號被包裹得)

jsonrequest = {'request': 'getJobs', 'token' : 'eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2'}; 

,或者如果jsonrequest具有內置因爲你可以使用jQuery.parseJSON()

字符串
jsonrequest = '{"request": "getJobs", "token" : "eb024fab2bf6a1bfb5863dcaabcfd63fcaea50e429237df3f1cbcbfcf9b2"}'; 
jsonrequest = $.parseJSON(jsonrequest); 
+0

感謝您的意見,但遺憾的是PHP方面仍然存在格式錯誤。 –

+0

即使在密鑰周圍添加了引號?你能記錄傳遞給PHP端的字符串嗎?那將是首先檢查哪部分是無效的JSON。 –