json_encode没有响应正确的json数据

时间:2012-06-18 05:29:01

标签: php jquery header json

假设我有2个.php文件:index.php和ajax.php

这是index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Website title</title>
<link href="css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"  src="jquery.js"></script>

</head>
<body>


<script type="text/javascript">
    ajax_register = function(){
        $.ajax({
            type: "post",
            url: 'ajax.php',
            dataType: 'json',
            data: "act=register",
            success: function(data){
                //var json = $.parseJSON(data);
                //alert(json);
                alert(data.result);

            },
            error: function(e){
                alert("Error : " + e);
            }
        });
    };

</script>

<form name="myForm" id="myForm" method="post">
    Your name :
    <input type="text" name="name" id="name" /> 
    <a href="javascript:void(0)" onclick="ajax_register()">Register</a>
</form>

</body>
</html>

这是ajax.php:

<?php
$result = array("result"=>"Success");
echo json_encode($result);
exit;

但我总是有“错误”警告对话框。

在Firebug中查看,我看到响应数据包含所有html元素而不是仅包含json数据。

尝试通过编辑ajax.php发送标题为“json”:

<?php
header("Content-type: application/json");
    $result = array("result"=>"Success");
    echo json_encode($result);
exit;

但它不起作用。响应标头始终为:Content-Type:text / html;

更新

这是ajax.php的回应:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <META http-equiv=Content-Type content="text/html; charset=utf-8">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Website title</title>
    <link href="css.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript"  src="jquery.js"></script>

    </head>
    <body>


    <script type="text/javascript">
        ajax_register = function(){
            $.ajax({
                type: "post",
                url: 'ajax.php',
                dataType: 'json',
                data: "act=register",
                success: function(data){
                    //var json = $.parseJSON(data);
                    //alert(json);
                    alert(data.result);

                },
                error: function(e){
                    alert("Error : " + e);
                }
            });
        };

    </script>

    <form name="myForm" id="myForm" method="post">
        Your name :
        <input type="text" name="name" id="name" /> 
        <a href="javascript:void(0)" onclick="ajax_register()">Register</a>
    </form>
{"result":"Success"}

我在这里错了什么?

谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

我只将此LOC <script type="text/javascript" src="jquery.js"></script>更改为

<script type="text/javascript"  src="http://code.jquery.com/jquery-latest.js"></script> 

检查你的代码及其在我的最终工作正常..检查JS错误可能是JS冲突的情况。 (告诉我成功)。如果问题仍然存在,请告诉我

index.php代码 -

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Website title</title>
<link href="css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"  src="http://code.jquery.com/jquery-latest.js"></script>

</head>
<body>


<script type="text/javascript">
    ajax_register = function(){
        $.ajax({
            type: "post",
            url: 'ajax.php',
            dataType: 'json',
            data: "act=register",
            success: function(data){
                //var json = $.parseJSON(data);
                //alert(json);
                alert(data.result);

            },
            error: function(e){
                alert("Error : " + e);
            }
        });
    };

</script>

<form name="myForm" id="myForm" method="post">
    Your name :
    <input type="text" name="name" id="name" /> 
    <a href="javascript:void(0)" onclick="ajax_register()">Register</a>
</form>

</body>
</html>

ajax.php CODE ---

<?php
$result = array("result"=>"Success");
echo json_encode($result);
exit;

点击注册时提醒Success

答案 1 :(得分:-1)

我认为你可以改变这一行

 data: "act=register",

 data: " act : register ",