javascript - How can Jquery's $.get() method bring cookies when sending a request?
黄舟
黄舟 2017-06-15 09:22:34
0
6
1361

How can Jquery's $.get() method bring cookies when sending a request?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
滿天的星座

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

typecho

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.

学习ing

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template