Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

Ajax请求返回,总是0问题。

操作平台:Windows10
环境平台:Apche+php+mysql
jQuery版本:v3.5.1

前台页面代码:

Js:

$(() => { 

 'use strict';

  // 提交
  $("#submit").click(() => {
        add();
  });

  function add() {
        $.ajax({
            type: "POST",
            url: "/admin/add",
            data: {
               title : $('title').val(),
               cate_pid : $('#cate_id').val()
            },
            dataType: 'json',
            async: false, 
            cache: false,
            success: function (result) {
                if (result.status == '1') {
                      window.location.reload();
                  };    
                },
                error: function (e) {
                    alert("请求异常");
                    alert(e.statusText);
                }
        });
    }

});

服务器PHP代码:

header('Content-Type:application/json; charset=utf-8');   

....msql保存数据省略,保存成功后以下返回1

$result = [
       'status' => 1,
       'msg' => '新增成功',
       'title'=>$_POST['title'],
       'pid'=>$_POST['cate_pid'],
        ];
       echo json_encode($result,JSON_UNESCAPED_UNICODE);

返回请求:

{
status: "0", 
msg: "新增成功", 
title: "素材", 
pid: "3"
}

为什么总是返回0,不是1 ?
求解。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

两种可能
1.你看错地方了
2.有全局处理接口返回的地方
3.前台处理了返回(不知道你放出来的是不是网络还是控制台输出的,看Network排查)
4.被劫持了(x)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...