2013-02-04 101 views
0

我的ajax请求中出现错误200。我想发布1个值,这是一个电子邮件地址到我的ajax请求中的页面。有人能告诉我什么是错PHP 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); 
     } 
    });/**/ 
}); 
+1

错误200? 200是成功的代码。从错误信息我怀疑你没有返回有效的JSON –

+0

什么说'getemailaddress.php'? –

+0

告诉我们你的'getemailaddress.php' – coolguy

回答

1

当你正在使用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 {} 
},