2016-05-15 58 views
0

我需要使用参数让一个url的后调用让我们说a =“x”,b =“y”,c =“z”和d是表单数据字段。这是我在HTML表单上传的文件。打一个阿贾克斯电话,我打电话。传递一个表单数据字段到POST调用

$.ajax({ 
     contentType: false, 
     processData: false 
     type: 'post', 
     url:"some url i put here", 
     data: { 
      a: "x", 
      b: "y", 
      c: "z", 
      } 
    }) 
    .done(function(data){ 
     alert("Data: ", data); 
    }); 

HTML部分是

<body> 
<input id="filey" type="file" size="40"><br><br> 
<button>Send an HTTP POST request to a page and get the result back</button> 

</body> 
</html> 

需要注意的是A,B,C都事先知道我。 我如何通过d?

+0

什么是您的服务器端语言?也可以在html页面添加表格 –

回答

0

我意识到你要求上传Ajax文件,而不仅仅是发送表单数据。这应该适合你:

var files; 

$('input[type=file]').on('change', function(event) { 
    files = event.target.files; 
}); 

$('form').on('submit', function(event) { 
    event.stopPropagation(); 
    event.preventDefault(); 

    // Notify the user that this may take a while... 

    var data = new FormData(); 
    $.each(files, function(key, value) 
    { 
     data.append(key, value); 
    }); 

    $.ajax({ 
      contentType: false, 
      processData: false 
      type: 'POST', 
      url:"some url i put here", 
      data: data, 
      cache: false, 
      dataType: 'json', 
      success: function(data, textStatus, jqXHR) { 
       if(typeof data.error === 'undefined') { 
        submitForm(event, data); 
       } else { 
        console.log('ERRORS: ' + data.error); 
       } 
      }, 
      error: function(jqXHR, textStatus, errorThrown) 
      { 
       console.log('ERRORS: ' + textStatus); 
      } 
      complete: function(jqXHR, textStatus, errorThrown) 
      { 
       // Notify the user that the upload process is complete! 
      } 
    }); 
}) 

您将需要处理UI交互,因为文件上传可能很耗时。代码中有2条评论标记了可以完成UI交互的地方。通常,这是显示或隐藏微调器。

+0

@KanikaMidha如果这回答了您的问题,请点击答案旁边的复选标记以接受它。复选标记会将颜色更改为绿色。 Upvotes也是非常值得赞赏的。 :d –