0

I'm writing some javascript that uses jQuery to make a webservice call.

  • The webservice is one that I've written (in C#), so I have full control over it.
  • The webservice is writing some values to a database.
  • The web service is not on the same domain as the web site
  • The web service takes a JSON.stringified parameter that contains all the values to commit

I have a couple of questions:

  1. If the webservice throws an exception (e.g. primary key constraint) will this cause a jQuery Ajax exception?

  2. If I want to return a value, say the number of rows affected, do I have to use JSONP (because it is cross-domain)?

Update: I have to support IE8, so CORS isn't an option. Thanks to @Volodymyr and @mcv for pointing out that option

2 Answers 2

1
  1. It depends how its done in c#, if it throws exception and response code would not be 200 then yes you will have error in jquery Ajax and you can add event for error, if c# doesnot throw exception then it will be success event on jquery, but probably you will got different response json with status. Since you have full control then you can do what you want.
  2. Still if you have control you can do following

    public class AllowCrossSiteAttribute : ActionFilterAttribute {
      public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) {
        if (actionExecutedContext.Response != null) {
          actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
          base.OnActionExecuted(actionExecutedContext);
        }
      }
    }
    

Add this to you code (Please not that here you allow all domain but you can replace "*" with list of your domains), then on web api controller add this as attribute

Or using web config

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type, Authorization" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
1
  • I have to support IE8 so CORS isn't an option. Sorry I didn't make that clear in my Q. but I think your first answer solves some of my problems Commented Jan 7, 2014 at 0:43
0
  1. If an Ajax call gets an error code from the server, your error() callback gets called instead of your success() callback. Depending on your server side framework, you might be able to catch the exception there and still pass some useful data about the error, together with the http error code. But if you don't handle it, the Ajax call will probably receive a pile of useless HTML with an error code. Whatever you do, make sure to send an error code in case of an error.

  2. On older browsers, yes. But you won't be able to do a POST with jsonp, so all your data has to go in the request parameters. If you only care about modern browsers, the better solution is to set a Access-Control-Allow-Origin header in your server response. See How does Access-Control-Allow-Origin header work? for more info.

2
  • Unfortunately, I have to support IE8, so CORS isn't really an option for me yet. I'm interested in more about the server-side exception handling: If I write something like (in C#) System.Web.HttpContext.Current.Response.Status=404 will that be picked up by the AJAX request? Commented Jan 7, 2014 at 0:41
  • I have no idea how C# handles http requests, but if the http response has a status of 404, JQuery will notice and call your error callback.
    – mcv
    Commented Jan 7, 2014 at 8:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.