Php 如何获取json值
我使用jQueryAjax调用PHP PHP ajax中的Success() 输出为Php 如何获取json值,php,json,Php,Json,我使用jQueryAjax调用PHP PHP ajax中的Success() 输出为 {"isSuccessful":true,"currentId":13} undefined 我犯了什么错误 您还需要发送正确的标题(在本例中为application/json): 或者,如果您想自己解析它,那么您可以使用,但请确保管理解析失败时引发的错误。您在ajax调用中丢失了数据类型,这是可以读取的 这通常不是必需的(例如,如果您设置了标题并让jquery猜测),但我建议确保jquery知道它正在接收
{"isSuccessful":true,"currentId":13}
undefined
我犯了什么错误 您还需要发送正确的标题(在本例中为
application/json
):
或者,如果您想自己解析它,那么您可以使用,但请确保管理解析失败时引发的错误。您在ajax调用中丢失了数据类型,这是可以读取的
这通常不是必需的(例如,如果您设置了标题并让jquery猜测),但我建议确保jquery知道它正在接收的数据并处理错误(如果有),而不是依赖另一个页面上使用另一种语言的脚本来确保数据的完整性。您也在使用jquery吗?你能用ajax显示更多的调用吗?$.ajax({type:“POST”,url:“php/registerDetails.php”,data:data,success:function(data){var currentData=data;console.log(currentData);var s=currentData.currentdid;console.log(s);},error:function(jqXHR,textStatus,errorshown){console.log(errorThrown);},});
1)
在浏览器中打开ajax url,确保它只有json字符串,没有其他额外字符<代码>2)在php中使用内容类型:json头3)
在jquery请求中将数据类型指定为json
success:function(data){
var currentData=data;
console.log(currentData);
var s=currentData.currentId;
console.log(s);
}
{"isSuccessful":true,"currentId":13}
undefined
$response = array('isSuccessful'=> true , 'currentId'=> $currentId);
$response = json_encode($response);
header('Content-Type: application/json');
echo $response;
$.ajax({
type:"POST",
url:"php/registerDetails.php",
data:data,
dataType:"json",
success:function(data){
var currentData=data;
console.log(currentData);
var s=currentData.currentId;
console.log(s);
},
error:function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
},
});