1

I'm writing digest authorization in phonegap
This is my code

Setting Authorization header in ajax

This is my code

$.ajax({  
  type: "GET",  
  url: "http://10.0.2.2:9000/",  
  beforeSend : function(req) {   
    req.setRequestHeader('Authorization','Digest username="username", realm="' + realm + '", nonce="' + nonce     + '", uri="/", response="' + response +'", opaque="' + opaque +'"'); 
  } 
}); 

I've already try using :

headers: { 'Authorization':'Digest username="username", realm="' + realm + '", nonce="' + nonce + '", uri="/", response="' + response +'", opaque="' + opaque +'"' } 

but it's still doesn't work. is there any method to set header? or any method that we can check if the header had set or not?

thanks :)

1 Answers1

0

beforeSend

Type: Function( jqXHR jqXHR, PlainObject settings ) A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and settings objects are passed as arguments. This is an Ajax Event. Returning false in the beforeSend function will cancel the request. As of jQuery 1.5, the beforeSend option will be called regardless of the type of request.

    beforeSend: function(xhrObj){
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Accept","application/json");
    }

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

https://gist.github.com/pithyless/1547408

Krish R
  • 22,583
  • 7
  • 50
  • 59
  • Did you checked response in firbug ->net tab >response panel – Krish R Nov 26 '13 at 07:09
  • yes , this is the screenshot.It return as "an empty string" https://www.dropbox.com/s/c9r4xtahleftmsy/ScreenShot040.bmp https://www.dropbox.com/s/l6j7x1rbsr9gs28/ScreenShot041.bmp $.ajax({ type: "GET", url: "http://localhost:8080/", error: function (request, textStatus, errorThrown) { var header = request.getAllResponseHeaders(); console.log("be"); console.log(header); – user3034844 Nov 26 '13 at 07:27
  • 401 Unauthorized error -The Web server (running the Web site) thinks that the HTTP data stream sent by the client (e.g. your Web browser or our CheckUpDown robot) was correct, but access to the URL resource requires user authentication 1) which has not yet been provided or 2) which has been provided but failed authorization tests. This is commonly known as "HTTP Basic Authentication". The actual authentication request expected from the client is defined in the HTTP protocol as the WWW-Authenticate header field – Krish R Nov 26 '13 at 07:31