解决ajax异步请求返回的是字符串问题

Ajax教程 2025-08-23

1.返回结果差异

js获取后打印res.code无结果。因为返回数据格式不正确。

php文件添加header头:

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

或者。j s 添加

contentType: "application/json; charset=utf-8",
dataType: "json",
$.ajax({
    url:'../data/insert_category.phparticle_type=' + value,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success:function(res){
    console.log('异步成功');
    console.log(res);
    console.log(res.code);
    },
    error:function(){
    console.log('异步失败');
    }
   })

总结