weixin_33691700 2017-01-06 18:29 采纳率: 0%
浏览 209

AJAX FormData()与encodeURI()

jQuery uses the encodeURI(), so the data sent through AJAX is like this:

key1=true & key2=34 & ...

To send an image through AJAX, I use the FormData(), so the data from AJAX (without an image) is like:

-----------------------------7e136023611f0
Content-Disposition: form-data; name="key2"

34

Now I have two pure-JS functions for AJAX. The main one uses encodeURI(), and the other one is only for uploading images, using the new FormData().

As I want to have only one AJAX function, my question is - would it be reasonable so switch only to the FormData()? What's the reason for jQuery to use the encodeURI()?

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2017-01-06 19:37
    关注

    jQuery $.ajax accepts an object as datastring, so you could pass on the url of the image, or also use something like:

    data = 'data:image/gif;base64,R0lGODlhAAEwAMQAAJ2M5Me98GRK1D...1rTIIAQA7';
    

    So you can then fetch and reconstruct the image on the other side.

    评论

报告相同问题?