Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquerymobile脚本可以读取良好的PHP格式_Php_Jquery_Json_Ajax - Fatal编程技术网

jquerymobile脚本可以读取良好的PHP格式

jquerymobile脚本可以读取良好的PHP格式,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我想知道我应该从PHP脚本返回哪种格式,以便此jquery脚本可用: success: function (resp) { $.mobile.loading("hide"); if (resp.success === true) { } else { } } 实际上,我是在php脚本中这样做的: $ar = array('success' => true); echo json_encode($ar); 这将返回以下内容:{“success”:

我想知道我应该从PHP脚本返回哪种格式,以便此jquery脚本可用:

 success: function (resp) {
    $.mobile.loading("hide");
    if (resp.success === true) {

    } else {

     }
}
实际上,我是在php脚本中这样做的:

$ar = array('success' => true);
echo json_encode($ar); 
这将返回以下内容:
{“success”:true}


但是我无法让它在ajax上工作。

您的ajax请求需要JSON(如果
数据类型:'JSON'
),但您不会通过PHP输出返回JSON,因为默认的内容类型是
text/html
,而不是
application/JSON
。在对JSON输出进行
echo
”之前添加适当的
Content-Type
标题

header('Content-Type: application/json');
echo json_encode(array('something' => 'else'));

AJAX请求需要JSON(如果
dataType:'JSON'
),但您不会通过PHP输出返回JSON,因为默认的内容类型是
text/html
,而不是
application/JSON
。在对JSON输出进行
echo
”之前添加适当的
Content-Type
标题

header('Content-Type: application/json');
echo json_encode(array('something' => 'else'));

这太糟糕了!谢谢@Tom,我的问题是php脚本的返回格式@pollux1er我很高兴能帮上忙:)这太糟糕了!谢谢@Tom,我的问题是php脚本的返回格式@pollux1er我很高兴能帮上忙:)