错误JSON数据通过从PHP到AJAX [英] wrong JSON data passes from PHP to AJAX

查看:168
本文介绍了错误JSON数据通过从PHP到AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从 json_en code 与读取数据的麻烦。 当我设置的行动,当前页和隔离两个部分code发送 JSON 数据它运作良好,但我改变了行动目标到另一个页面,例如后 procees.php ,把回声 json_en code 在$ C $的部c它返回的孔 HTML code数据变量和 JS 警报2,似乎是 JSON 数据得到错误的值 可有人告诉我,我有什么错?

的index.php code:

 < HTML>
    < HEAD>

    < /头>
 &所述;脚本的src =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js>&所述; /脚本>

    &所述;脚本的src =submit.js>&所述; /脚本>
<身体GT;

    <形式的行动=process.php方法=邮报>
    <输入名称=名称类型=文本/>
    <输入名称=家庭式=文本/>
    <输入名称=年TYPE =号最大长度=4分=输入1300最大=1393/>
    <输入名称=月类型=数字最大长度=2分=1最大=12/>
    <输入名称=日式=号最大长度=2分=1最大=31/>
    <输入值=提交类型=提交/>

    < /形式GT;
< /身体GT;
< / HTML>
 

和process.php code

 标题(内容类型:应用程序/ JSON);
    包括(classes.php);

        / *声明中的所有对象* /
        /* 初始化 */
        $名=修剪($ _ POST [名称]);
        / *和最初所有的变量* /

                如果(($入住>!alphabetSyntaxChecking($名))){
         $错误['名称'] =请检查你插入;
        }
          / *验证另一个输入... * /


            如果(!空($错误)){
            $数据['成功'] = FALSE;
            $数据['错误'] = $错误;
              } 其他 {
            $数据['成功'] =真;
            $首诗= $ fileContent-> readFromFile(一下poem.txt,$依然存在,$ moshakhasat);
                $ allPoem = $诗[1];
                $数据['allpoem'] = $ allPoem;
        }
            回声json_en code($的数据);
 

和阿贾克斯code

  $(文件)。就绪(函数(){
$(形式)。递交(函数(事件){
$阿贾克斯({
            键入:POST,
            网址:process.php,
            数据:$(本).serialize()
            数据类型:JSON,
            EN code:真

        })
    .done(功能(数据){
    警报(1);
    })
    .fail(功能(数据){
           警报(2);
            });
。事件preventDefault();
});
});
 

解决方案

删除json_en code($的数据),以确保什么是process.php文件的回报,如果别的是         数据可能是从classes.php文件,这就是为什么ü得到的响应html标签

  $(文件)。就绪(函数(){
    $(形式)。递交(函数(事件){
    $阿贾克斯({
                键入:POST,
                网址:process.php,
                数据:$(本).serialize()
                数据类型:JSON,
                EN code:真

            })
        .success(功能(数据){
         的console.log(数据);
        })
        .error功能(jqxhr)
                {
                 //这将是错误的:324,500,404或anythings其他
                 执行console.log(jqxhr.responseText);
                }
    。事件preventDefault();
    });
    });
 

I'm having trouble with reading data from json_encode. when I set action to current page and isolate the two part of code for sending json data It works well, but after I change the action destination to another page for example procees.php and put echo json_encode at the end of the code It returns hole of Html code in data variable and the js alert '2' and seems that json data get wrong value can somebody tell me where I go wrong?

index.php code :

<html>
    <head>

    </head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

    <script src="submit.js" ></script>
<body>

    <form action="process.php" method="post">
    <input  name  = "name"    type = "text"  />
    <input  name  = "family"  type = "text"  />
    <input  name  = "year"    type = "number" maxlength="4" min="1300" max="1393"/>
    <input  name  = "month"   type = "number" maxlength="2" min="1" max="12"/>
    <input  name  = "day"     type = "number" maxlength="2" min="1" max="31"/>
    <input  value ="submit"   type = "submit" />

    </form>
</body>
</html>

and process.php code

header("Content-type: application/json");
    include("classes.php");

        /* declare all objects */
        /* Initialization */
        $name    =  trim( $_POST["name"] );
        /*and Initial all the variables*/

                if (!($check->alphabetSyntaxChecking($name))) {
         $errors['name'] = "please check your inserts";
        }
          /*validate another inputs ... */


            if ( ! empty($errors)) {
            $data['success'] = false;
            $data['errors']  = $errors;
              } else {
            $data['success'] = true;
            $poem = $fileContent->readFromFile("poem.txt", $remain, $moshakhasat);
                $allPoem = $poem[1];
                $data['allpoem'] = $allPoem;    
        }
            echo json_encode($data);

and Ajax code

$(document).ready(function() {
$('form').submit(function(event) {
$.ajax({
            type        : 'POST', 
            url         : 'process.php', 
            data        : $(this).serialize(),
            dataType    : 'json',
            encode      : true

        })
    .done(function(data) {
    alert("1");
    })
    .fail(function(data) {
           alert("2");
            });
event.preventDefault();
});
});

解决方案

Remove json_encode($data) to be sure what is return from process.php file,if anything else is in data that may be from classes.php file , that's why u get html tags on response

    $(document).ready(function() {
    $('form').submit(function(event) {
    $.ajax({
                type        : 'POST', 
                url         : 'process.php', 
                data        : $(this).serialize(),
                dataType    : 'json',
                encode      : true

            })
        .success(function(data) {
         console.log(data);
        })
        .error function(jqxhr)
                {
                 //it  will be errors: 324, 500, 404 or anythings else
                 console.log(jqxhr.responseText); 
                }
    event.preventDefault();
    });
    });

这篇关于错误JSON数据通过从PHP到AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆