2

I have a code which tries to post to a api. But I keep getting 401 in firefox and 400 in safari

      $.ajax({
        type: "POST",
        url: "http://url.com",
        dataType: "json",
        async: "false",
        data: form_data,
        beforeSend: function(xhr1) {
          xhr1.setRequestHeader("Authorization", "Basic " + encodeBase64(username + ":" + password))
        },
        success: function (data,status,xhr){
          //do something
        }
      });

I followed instructions from here http://dothow.blogspot.com/2009/05/http-basic-authentication-with-jquery.html?showComment=1270999665472#c3021624182011440325

*encodeBase64 is part of a library function

3
  • Observe the request headers using Firebug or similar. Then make the request directly in a web page without using XMLHTTPRequest. Compare the headers to spot the differencee. Commented Oct 18, 2011 at 9:38
  • 1
    Do you perhaps want to use username and password properties of $.ajax? api.jquery.com/jQuery.ajax Commented Oct 18, 2011 at 9:52
  • I tried username and password as well. In firefox I get a big error Error: uncaught exception: [Exception... "Success arg 1 [nsIDOMDocumentXBL.getBindingParent]" nsresult: "0x0 (NS_OK)" location: "JS frame :: chrome://global/content/bindings/tree.xml :: onxblunderflow :: line 947" data: no] and safari the same 400 bad request Commented Oct 18, 2011 at 11:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.