JQuery ajax File Download _ajax

Source: Internet
Author: User

Generally used under the file

window.location.href= ' xxx '  
But sometimes passing parameters to the background requires

window.location.href= ' xxx?name=123&age=234 '
It's a little unreasonable to have too many passes or too much length of argument.

jquery.download = function (URL, data, method) {//Get URL and data
    if (URL && data) { 
        //data is string or array/ Object
        data = typeof data = = ' string '? Data:jQuery.param (data);//input
        var inputs = ' To assemble parameters into form;
        Jquery.each (Data.split (' & '), function () { 
            var pair = this.split (' = ');
            inputs+= ' <input type= "hidden" name= "' + pair[0] + '" value= "' + pair[1] + '"/> '; 
        } "; Request to send requests for
        jQuery (' <form action= ' + URL + ' method= ' + (method| | ') Post ') + ' > ' +inputs+ ' </form> '). Appendto (' body '). Submit (). remove ();};
$.download (' url ', ' data ', ' post ');	
This method downloads files by creating a from form for mock data requests

which uses the JQuery Ajax-param ()

var params = {width:1900, height:1200};
var str = Jquery.param (params);
Console.log (str)
width=1680&height=1050//Output results
The Param () method creates a serialization representation of an array or object, specifically to look up the manual.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: