2014-02-05 127 views
1

我正在编写测试AJAX图像和数据上传的代码。然而,阿贾克斯似乎返回一个错误,表明输入无效:Ajax错误:“意外的输入结束”

Syntax error: unexpected end of input

脚本和HTML如下

<input type="file" name="vdofile" id="file" /> 
<input type="text" id="name" /> 

<button id="send">Send</button> 
$("#send").click(function(){ 

    var form = new FormData(); 

    form.append('vdofile',$("#file").prop("files")[0]); 
    form.append('name', $("#name").val()); 

    $.ajax({ 
     url: 'upload.php', 
     type: "POST", 
     data: form, 
     dataType: 'json', 
     contentType: false, 
     processData: false, 

     success: function(data) 
     { 
      alert(data.message); 
     },  
     error: function(xhr, status, error) 
     { 
      alert('An error occured.. ' + xhr.responseText + '..' + error); 
     } 

    });// ajax 
}); // function 

测试PHP如下:

header("Content-Type: application/json"); 

if(isset($_FILES['vdofile']) && isset($_POST['name'])) 
{ 
    $result = array(
     'message' => 'both data and file received' 
    ); 
} 
else { 
    $result = array(
     'message' => 'one of parameter missing' 
    ); 
} 
return json_encode($result); 

回答

9

你的PHP返回一个空的响应。 return不打印任何东西。

print json_encode($result); 

应该会更好。

+0

谢谢.. :)似乎工作。已经使用'echo'而不是'print' –

相关问题