2013-02-04 2 views
0

Im가 내 아약스 요청에 200 오류가 발생합니다. 내 아약스 요청의 페이지에 이메일 주소 인 1 개의 값을 게시하고 싶습니다. 누군가가 잘못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 데이터를 보내는 것을 잊지 때문에 작동하지 않습니다 :이 같은

당신의 JQuery와 콜백에 따라서
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; 
} 

당신이 반환받을 데이터 :

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