84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
How can Jquery's $.get() method bring cookies when sending a request?
人生最曼妙的风景,竟是内心的淡定与从容!
Under the same domain, cookies will be automatically included
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
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
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.
cookies
Application
get
cookie
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
**$.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.
Under the same domain, cookies will be automatically included
First of all, jquery’s get method and post method are both encapsulations of ajax, see the source code
} );`
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
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
First search for the
cookies
option in the consoleApplication
to see if thecookies
settingsget
were successful. If it is set up, the client will automatically bringcookie
to every page under the same domain name.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
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.