为什么我的JSON响应页面充满源代码 [英] Why is my json response a page full of source code

查看:92
本文介绍了为什么我的JSON响应页面充满源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的复选框,单击它会将XHR发送到PHP页面,php进程正确,并且我使用json_encode($response)返回.但是,我得到的不是页面的源代码,不是简单的true或false,这当然会导致"parsererror". ajax调用如下

I have a simple checkbox, on click it sends XHR to PHP page , php processes correctly and I use json_encode($response) to return. But instead of a simple true or false I get the source code for the page and it is causing a "parsererror" of course. ajax call as follows

$.ajax({
type: "post",
url: "myprocessor.php",
dataType: 'json',
data: { "id" : idnumber, "action": "makeLive", "isLive" : "1" },
beforeSend: function(data) {
            $("#ajaxInProgress").addClass('progress');
        },
        success: function(data) {
            $("#status").removeClass().addClass((data.error === true) ? 'error' : 'success').text('Success! Appraiser is NO LONGER Live ').slideDown('slow');
        },
        error: function(data) {
            $("#status").removeClass().addClass('error').text(' - Changing the Live status for this appraiser to "Not Live" has failed - APPRAISER IS STILL LIVE IN SYSTEM, please try again').slideDown('slow');
        },

        complete: function(data) {
            $("#ajaxInProgress").removeClass('progress');
            setTimeout(function() {
                $("#status").slideUp('slow').removeClass();
            },2000);
        }
    }); 

我发布到的php如下:

The php I post to is as follows:

if (isset($_POST['action'])) { 
if($_POST['action']=='makeLive') { 
    $checkappraiser=mysql_query("SELECT * FROM table WHERE id='".mysql_real_escape_string($_POST['id'])."'");
    if (mysql_numrows($checkappraiser)>0) {
        $livesetting=mysql_result($checkappraiser,0,"live");
        $livesetting=!$livesetting;
        $runSql = mysql_query("UPDATE table SET live='$livesetting' WHERE id='".mysql_real_escape_string($_POST['id'])."'");
        if(!$runSql) {
            $return['error'] = true;
        } else { 
            $return['error'] = false;   
        }
    }
}   
echo json_encode($return);
}

任何建议都会很棒. 我正在传递正确的数据 我正在数据库中更新正确的数据 我的响应作为解析器错误返回,因为它试图将源代码解析为json数组.

Any suggestions would be great. I am getting the proper data passed I am getting the correct data updated in DB My response is coming back as a parser error because it is trying to parse the source code as a json array.

推荐答案

快速检查一下,您是否将<?php放在php文件的开头?

Just a quick check, do you put <?php at the beginning of your php file?

这篇关于为什么我的JSON响应页面充满源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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