Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding request header to jquery ajax call throws 101 exception
    text
    copied!<p>Hi I have simplified my code as much as possible below. What I am trying to achieve is call a restful wcf service.However when I add the set request header method I get exception, in both Firefox and chrome, but it works in IE it successfully hits the service method.</p> <pre><code>&lt;script src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-md5.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function WriteResponse(string) { $("#divResult").val(string); } function setHeader(xhr) { var secretkey = "1234dgt"; var hashedUrl = CryptoJS.HmacMD5($('#txtUrl').val(), secretkey); var hashedUrlBase64 = hashedUrl.toString(CryptoJS.enc.Base64); xhr.setRequestHeader('Authorization', hashedUrlBase64, "1234dgt"); } $(document).ready(function () { $("#btnCall").click(function () { jQuery.support.cors = true; $.ajax({ url: $('#txtUrl').val(), type: 'GET', async: false, dataType: 'json', success: function (data) { WriteResponse(data); }, error: function (x, y, z) { alert(x + '\n' + y + '\n' + z); }, beforeSend: setHeader }); }); }); &lt;/script&gt; </code></pre> <p>Any help would be much appreciated.<br> Thanks</p> <pre><code>// Here is the c# code used to call the same service: public static string GetUserBookmarks(string uri) { HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest; string encodedUri = EncodeText(_key, uri, UTF8Encoding.UTF8); request.Headers[HttpRequestHeader.Authorization] = encodedUri; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream bookmarksStream = response.GetResponseStream(); StreamReader reader = new StreamReader(bookmarksStream); string str = reader.ReadToEnd(); reader.Close(); bookmarksStream.Close(); return str; } public static string EncodeText(string key, string text, Encoding encoding) { HMACMD5 hmacMD5 = new HMACMD5(encoding.GetBytes(key)); byte[] textBytes = encoding.GetBytes(text); byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes); string encodedText = Convert.ToBase64String(encodedTextBytes); return encodedText; } // Here is the wcf service method and its interface\contract [ServiceContract] public interface IRestSerivce { [WebGet(UriTemplate = "users/{username}")] [OperationContract] List&lt;string&gt; GetUserBookmarks(string username); } public List&lt;string&gt; GetUserBookmarks(string username) { WebOperationContext context = WebOperationContext.Current; OutgoingWebResponseContext outgoingResponseContext = context.OutgoingResponse; bool isUserAuthenticated = IsUserAuthenticated(username); if (isUserAuthenticated == false) { outgoingResponseContext.StatusCode = HttpStatusCode.Unauthorized; return null; } outgoingResponseContext.StatusCode = HttpStatusCode.OK; List&lt;string&gt; bookmarks = new List&lt;string&gt;(); bookmarks.Add("User has been authenticated Successfully"); return bookmarks; } </code></pre> <p>Here is the error I am receiving, this is the alert in the ajax error function producing this, however there are no errors in the firbug console window.</p> <p>[object Object] error [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: <a href="http://code.jquery.com/jquery-1.9.1.min.js" rel="nofollow">http://code.jquery.com/jquery-1.9.1.min.js</a> :: .send :: line 5" data: no]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload