87

According to Requests with credentials, Firefox will only send credentials along with cross-domain posts if

invocation.withCredentials = "true";

is set… But it doesn't seem like jQuery's Ajax API provides any mechanism for this.

Is there something I've missed? Is there some other way I can do it?

3 Answers 3

183

Functionality is supposed to be broken in jQuery 1.5.

Since jQuery 1.5.1 you should use xhrFields param.

$.ajaxSetup({ type: "POST", data: {}, dataType: 'json', xhrFields: { withCredentials: true }, crossDomain: true }); 

Docs: http://api.jquery.com/jQuery.ajax/

Reported bug: http://bugs.jquery.com/ticket/8146

Sign up to request clarification or add additional context in comments.

5 Comments

Is this supposed to work for cross (non-subdomain) ajax requests as well?
I'm still getting prompted for credentials
@JohnGrabanski Did you fix your issue?
I'm confused, where do you add the actual credentials?
@garek007 they come in cookies, auth headers, or via other means.
41

You can use the beforeSend callback to set additional parameters (The XMLHTTPRequest object is passed to it as its only parameter).

Just so you know, this type of cross-domain request will not work in a normal site scenario and not with any other browser. I don't even know what security limitations FF 3.5 imposes as well, just so you don't beat your head against the wall for nothing:

$.ajax({ url: 'http://bar.other', data: { whatever:'cool' }, type: 'GET', beforeSend: function(xhr){ xhr.withCredentials = true; } }); 

One more thing to beware of, is that jQuery is setup to normalize browser differences. You may find that further limitations are imposed by the jQuery library that prohibit this type of functionality.

3 Comments

According to api.jquery.com/jQuery.post it should be type: "GET" and not method: 'GET' I tripped over it when using your example
@Xosofox I know this is an old comment, but as of jQuery 1.9, method: 'GET' is supported. api.jquery.com/jquery.ajax
Note that this no longer works in jQuery 3+, because (a) the api to this function has changed, and (b) it no longer has access to the XHR object, which is created after this function runs. Instead, you should use xhrFields.
3

In jQuery 3 and perhaps earlier versions, the following simpler config also works for individual requests:

$.ajax( 'https://foo.bar.com, { dataType: 'json', xhrFields: { withCredentials: true }, success: successFunc } ); 

The full error I was getting in Firefox Dev Tools -> Network tab (in the Security tab for an individual request) was:

An error occurred during a connection to foo.bar.com.SSL peer was unable to negotiate an acceptable set of security parameters.Error code: SSL_ERROR_HANDSHAKE_FAILURE_ALERT

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.