简体   繁体   English

PHP ajax请求返回字符串

[英]PHP ajax request return string

Im getting an error 200 in my ajax request. 我在ajax请求中收到错误200。 Im wanting to post 1 value which is a email address to the page in my ajax request. 我想发布1值,这是我的ajax请求中的页面的电子邮件地址。 Could someone tell me what is wrong 有人可以告诉我怎么了

Error: 错误:

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

JQuery 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); 
        }   
    });/**/
});

As you are using json data type any returned data from server must be in that format. 当您使用json数据类型时,从服务器返回的任何数据都必须采用该格式。

So FIRST Don't forget that you send post data, so work with: 所以首先请不要忘记发送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;
}

So in your jquery callback you get returned data like this: 因此,在您的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