简体   繁体   English

使用jQuery Ajax提交表单时出现响应错误

[英]Response error in submitting a form using jquery ajax

M having problem with getting resposnse from the server.M using Jquery Ajax I want to upload a file using ajax.But the follwing lines of codes are not working page1: M在从服务器获取资源时遇到问题。使用Jquery Ajax我想使用ajax上传文件。但是以下几行代码无法正常工作:

<script language="javascript" src="jquery-1.7.1.js"/></script>
<script language="javascript" src="jquery.form.js"/></script>
<script language="javascript">
$(document).ready(function(){
  $('#photoimg').change(function(){
    var fd=new FormData();
    $.ajax({
      type:'POST',
      url:'NewuploadScript.php',
      data:fd,
      contentType: 'application/x-www-form-urlencoded',
      async:true,
      cache:false,
      processData: true,
      success:function Result(data2) {
        document.write(data2);
      }
    }); 
  });
});
</script>
<form> 
<input type="file"  id="photoimg" name="file" multiple>
<input type="submit" id="BtnSbmt" value="Upload"/>
</form>

Pag2: Pag2:

<?php
echo $name=  basename($_FILES['photoimg']['name']);
echo $size=  basename($_FILES['photoimg']['size']);
?>

when I run the codes jquery display the following error: 当我运行代码jquery时,显示以下错误:

Uncaught TypeError: Illegal invocation
jQuery.extend.param.addjquery-1.7.1.js:7601
buildParamsjquery-1.7.1.js:7658
jQuery.extend.paramjquery-1.7.1.js:7621
jQuery.extend.ajaxjquery-1.7.1.js:7467
(anonymous function)ImageUploader.php:18
jQuery.event.dispatchjquery-1.7.1.js:3256
jQuery.event.add.elemData.handle.eventHandlejquery-1.7.1.js:2875

It's hard to tell what the error message means exactly without seeing what comes back in your Ajax exchange. 在看不到Ajax交换返回的内容的情况下,很难说出错误消息的确切含义。 Your PHP code should not read 您的PHP代码不应该阅读

echo $var_name = ...

because that is an assignment. 因为那是一项任务。 Try the following... 尝试以下...

<?php
    echo basename($_FILES['photoimg']['name']);
    echo basename($_FILES['photoimg']['size']);
?>

Or just 要不就

print_r($_FILES);

for starters edit : 对于初学者编辑:

success:function (data2) {
        document.write(data2);
      }

Maybe the problem is related with FormData see this 也许问题与FormData有关,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM