How can Jquery's $.get() method bring cookies when sending a request?
伊谢尔伦2017-06-15 09:24:34
First of all, jquery’s get method and post method are both encapsulations of ajax, see the source code
jQuery.each( [ "get", "post" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
// Shift arguments if data argument was omitted
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
// The url can be an options object (which then must have .url)
return jQuery.ajax( jQuery.extend( {
url: url,
type: method,
dataType: type,
data: data,
success: callback
}, jQuery.isPlainObject( url ) && url ) );
};
} );`
Then, how can I bring cookies when sending a request?
When the ajax method sends a request, it will automatically bring the cookie of the domain name you logged in, and you do not need to set it.
So, Jquery’s $.get() method will always bring cookies
typecho2017-06-15 09:24:34
When used, the client will automatically bring the cookie, and jquery has been encapsulated. If you want to customize the cookie yourself, you can use the $.cookie plug-in to set the client cookie and finally get it.
For debugging, you can open the console F12 and view the information in the request header on the network
巴扎黑2017-06-15 09:24:34
First search for the cookies
option in the console Application
to see if the cookies
settingsget
were successful. If it is set up, the client will automatically bring cookie
to every page under the same domain name.
学习ing2017-06-15 09:24:34
When sending a request, the browser will automatically carry the cookie and pass it to the background. Only the localStorage/sessionStorage parameters need to be passed asynchronously as parameters
为情所困2017-06-15 09:24:34
**$.support.cors = true;**
$.ajax({
url: urls.getDetailList,
type: "get",
dataType: "json",
**xhrFields: { withCredentials: true },**
success: function(res) {}
})
You should have $.support.cors cross-domain not turned on. Just add the end code withCredentials. In addition, the backend must also be configured with cross-domain.