Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php/ajax:json data.message返回未定义_Php_Jquery_Ajax_Json - Fatal编程技术网

php/ajax:json data.message返回未定义

php/ajax:json data.message返回未定义,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我在一个php文件中创建了一个表单,并使用ajax将请求发送到另一个php文件(test2.php) 当我将数据返回到ajax并希望在id=“txtHint2”(段落标记)中打印时,它显示未定义 有什么问题? 这是我的ajax文件代码: function add_product_category_main_form(){ var formdata={ 'name': $('input[name=product_group_name]').val() }; $.ajax({

我在一个php文件中创建了一个表单,并使用ajax将请求发送到另一个php文件(test2.php) 当我将数据返回到ajax并希望在id=“txtHint2”(段落标记)中打印时,它显示未定义
有什么问题?
这是我的ajax文件代码:

function add_product_category_main_form(){

var formdata={
'name':     $('input[name=product_group_name]').val()
};

    $.ajax({
        url:        "http://localhost/mysite/admin/php/test2.php",
        type:       "post",
        data:       formdata,
        datatype:   "json"

    })

    .done(function(data){

            document.getElementById('txtHint').innerHTML=data;
            document.getElementById('txtHint2').innerHTML=data.message;
        if( !data.success ){
            window.alert(data.errors.name);
        }
        else{           
            alert(data.message);
        }

    })

    .fail(function(data){
        alert("ajax failed");
    });

}
这是test2.php

<?php
include('../includes/db_connection.php');
$errors = array();
$data = array();

if( empty($_POST['name']) )
$errors['name'] = "Name is Required!";

if( !empty($errors) ){
$data['success'] = false;
$data['errors'] = $errors;

}else{

$name=$_POST['name'];

mysql_query("INSERT INTO product_group_main(product_group_main_name) VALUE ('$name')") or die(mysql_error());
mysql_close();

$data['success'] = true;
$data['message'] = "successfully inserted";
$data['name'] = $name;
}
echo json_encode($data);
?>


所有其他部分,例如insert into database工作正常

Javascript区分大小写
datatype
应该是
datatype

var data=JSON.parse(data.responseText);console.log(data.message)
以及如何使用此代码?我是phptry的新手,你的url:yourpAddress:port/mysite/admin/php/test2.php