jQuery way to achieve ajax interface call

0. serialize()     serializeArray()

  // jQuery order that we can form a simple operation, two methods are provided for processing form data
        // 1 serialize()
        // - may be a form of data into urlencoded form data
        // - can also be used directly as a parameter of the $ .ajax
        // 2 serializeArray()
        // - may be a form of data into an array
        // - can also be used directly as a parameter of the $ .ajax

        // Summary:
        // - if only the form data processing is needed, directly serialize () to (common)
        // - If you want to view the data content, you can use serializeArray ()

        // - before we say good form submission with just that after the submission will jump lead to refresh the page, the user experience is not good, too many times Request
        // - but the form itself is irreplaceable functions, but also with the use

  Note: When sending a request by $ .ajax FormData, specify two properties, does not cause a given set

  contentType: false; // body content type designation request

  processData: false; // data processing operation is not performed

  

FormData a DOM object requires parameters, the need to convert to use

 1. 

<body>
    <form id="form">
	<input type="text" name="username">
	<input type="file" name="file">
	<input type="button" value="按钮" id="btn">
    </form>
</body>
<script>
    $('#btn').on('click', function(){
        var fd = new FormData($('form')[0])
        $.ajax({
            method: 'post',
            url: '',
            data: fd,
            contentType: false,
            processData: false,
            success: function(res){
                console.log('res')
            }
        });
})

 2. 

 jsonp and no association ajax, jQuery order to unify when we requested operation, will be provided in jsonp send in a $ .ajax 

$ ( 'BTN #') ON ( 'the Click', function () {. 
	$ .ajax ({ 
		Method: 'the POST' 
		URL: 'HTTP: //localhost/Ajax_day4/demo1.php', 
		Success: function (DATAS) { 
			the console.log (DATAS); 
		}, 
            // If 'jsonp' in dataType, a request is sent to indicate the current jsonp form, regardless of the original function dataType 
		dataType: 'jsonp' 
	});    
}

  

 3. ajax setting request header

.ajax $ ({ 
         type: type, 
         headers: { 
			'token': 'token_value' 
         }, 
         URL: URL, 
         Data: Data, 
         Success: function (Data) { 
         }, 
         error: function (ERR) { 
         }, 
         Complete: function (XMLHttpRequest, status) {// the request after completion of the final execution parameter 
         } 
});

  

  

Guess you like

Origin www.cnblogs.com/xhrr/p/ajax.html