jquery ajax php json_encode

时间:2018-03-23 22:47:25

标签: php jquery ajax

JS

var data = {item:'value'}
$.ajax({
    async: true,
    type: 'POST',
    url: '/',
    data: data,
    dataType: 'json',
    error: function(jqXHR,status,text){
        console.log(jqXHR,status,text);
    },
    success: function(ajax_response){
        console.log(ajax_response);
    }
});

PHP

<?php
    if($_POST["item"] == "value"){
        $ajax_response = [];
        $ajax_response["response1"] = 'value1';
        $ajax_response["response2"] = 'value2';
        echo json_encode($ajax_response);
    }   
?>

所有似乎都没问题,但抛出了一个parsererror语法错误:

  

readyState的:4

     

responseText的:&#34; [&#34;假&#34;] {&#34;响应1&#34;:&#34;值1&#34;&#34;响应2&#34;:&#34 ;值2&#34;}↵↵&#34;

     

状态:200

     

状态文本:&#34; OK&#34;   ...   parsererror SyntaxError:意外   令牌{在JSON的第9位

我也试过json_last_error()但是返回0JSON_ERROR_NONE没有发生错误)

我不明白为什么json_encode()在json字符串结果之前添加["false"]

0 个答案:

没有答案
相关问题