繁体   English   中英

PHP ajax请求返回字符串

[英]PHP ajax request return string

我在ajax请求中收到错误200。 我想发布1值,这是我的ajax请求中的页面的电子邮件地址。 有人可以告诉我怎么了

错误:

message=:[object Object], text status=:parsererror, error thrown:=SyntaxError: JSON.parse: unexpected end of data

jQuery查询

$('#checkemail').click(function() {
    $.ajax({
        url:'http://' +  location.host + '/buyme/include/getemailaddress.php',
        type:'POST',
        contentType: "application/json; charset=utf-8",

        data: {email:$('#email').val()},
        dataType:"json",
        success: function(msg) {
            alert(msg);
        },
        error: function(ms, textStatus, errorThrown) {
            alert(errorThrown); 
        }   
    });/**/
});

当您使用json数据类型时,从服务器返回的任何数据都必须采用该格式。

所以首先请不要忘记发送post数据,所以请使用:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $email = $_POST['email'];
    // ...

    // and SECOND return suitable data type, ie, a json coded string.
    echo json_encode($your_result);

    // where $your_result can be simple as

    // $your_result['result'] = true;
    // $your_result['result'] = false;

    // a message
    // $your_result['msg'] = 'all OK';

    // a message and and a flag
    // $your_result['msg'] = 'all OK';
    // $your_result['result'] = true;
}

因此,在您的jquery回调中,您将获得如下返回的数据:

success: function(data) {
    if (data.msg != "") alert(data.msg);
    if (data.result === true) {} else {}
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM