简体   繁体   English

带有参数的jQuery ajax发布

[英]jquery ajax post with params

I need to upload file using JQuery's $ajax function, besides uploading the file, i want be able to tell which folder/directory this filed uploaded to. 我需要使用JQuery的$ ajax函数上传文件,除了上传文件之外,我还想知道此文件上传到的文件夹/目录。 i tried the following but it won't work: 我尝试了以下方法,但无法正常工作:

  data = new FormData()
  data.append('csv', fileData)
  $.ajax
    cache: false
    contentType:false
    processData: false
    type: 'POST'
    dataType: 'script'
    url: '/api/upload/'
    params: ['test']   <-- folder/directory i want to upload to
    data: data

    complete: (res) =>
      if not (res.status is 200)
        console.log(res.responseText)
        return false
      else
        return true

the api worked fine, but when check the request it always showed the params as empty: api工作正常,但是在检查请求时,它总是将参数显示为空:

params: []
query: {}

how can i pass extra parameter to ajax post? 我如何将额外的参数传递给ajax post? thanks 谢谢

You can add the additional param to the formData 您可以将其他参数添加到formData

data = new FormData()
data.append('csv', fileData)
data.append('path', 'my/path')

There is no option called params in $.ajax() $ .ajax()中没有称为params选项

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

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