user1324855
user1324855

Reputation: 73

jQuery ajax POST call to ASP.NET Web Service randomly fails

I'm developing a web site for mobile devices that makes ajax calls using jQuery (v 1.7.2) to an ASP.NET (v 2.0.50727) Web Service.

The call works correctly about 95% of the time, but it will randomly fail, returning a 500 internal server error. It fails on the server side before the first line of code is ever executed (the first line writes to the event log).

I haven't seen the call fail using a desktop browser that I remember, but I've seen it fail enough using an iPad. I added

<browserCaps userAgentCacheKeyLength="256">

to the Web Service's web.config file, but that hasn't helped.

javascript:

$.ajax({
  type: "POST",
  url: serverURL + "/getImage",
  data: '{"formURL":"' + url + '", "rowNumber":"'+rowNumber+'"}',
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (msg,textStatus, jqXHR) {
    ...
  }, error: function(xhr, ajaxOptions, thrownError) {
    ...
  }
}).done(function(){
  console.log("getImage call is done");
});

Example data passed to the web service:

'{"formURL":"fileName.xml", "rowNumber":"1"}'

c#

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getImage(string formURL, string rowNumber) {
  log("Retrieving image of form " + formURL);
  string image = "";
  string username = /*retrieve username*/;
  string password = /*retrieve password*/;
  if (username != null && username != "") {
    image = /*code to retrieve the image*/;
  }
  return image;
}

private void log(string message) {
  EvLog.WriteToEventLog(DateTime.Now.ToString("MM:dd:yyyy H:mm:ss:fff") + Environment.NewLine + message, 10);
}

The only thing I've found that has slightly helped me, is when the call fails because the response headers from the Web Service contain "jsonerror: true" though I haven't been able to pinpoint why it would randomly fail.

Any help is appreciated!

Upvotes: 1

Views: 1826

Answers (2)

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

Don't build your data in this way

data: '{"formURL":"' + url + '", "rowNumber":"'+rowNumber+'"}',

It can cause malformed JSON string.

Instead of this stringify your JavaScript object using JSON.stringify method:

data: JSON.stringify({formUrl: url, rowNumber: rowNumber}),

JSON.stringify will make all job for you to represent your object as valid JSON string.

Upvotes: 1

Jay S
Jay S

Reputation: 7994

Assuming it truly is a JSON error, my first thought is that the data being passed into the parameters is incorrect.

The following line is quoting the contents of variables, which I assume is being loaded from somewhere else in the code:

data: '{"formURL":"' + url + '", "rowNumber":"'+rowNumber+'"}',

Assuming you are already making sure rowNumber is an integer value and wouldn't break it, the likelihood is that the 'url' variable is breaking your JSON format. The easiest way this could happen is if you had an unescaped quote in the filename, especially if it's closing your parameter values earlier than expected when it gets concatenated.

There's always the possibility of the characters not being valid for the charset. Do you have an example data that triggers the failure? The example provided looks nice and clean, so I'm assuming it wasn't one of the error cases.

Upvotes: 0

Related Questions