簡體   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