Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:13:14+00:00 2026-06-07T05:13:14+00:00

I have some jQuery that makes a call into a WCF service. When the

  • 0

I have some jQuery that makes a call into a WCF service. When the service runs into an error, it throws a WebFaultException with a description and a 500 Internal Server Error status code.

Client side, I’m doing something along the lines of:

$.ajax({
    url: BaseUrl + 'ThisShouldThrowAnException',
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    success: function (data) {
        // this should never be called
        alert("Got data: " + data);
    },
    error: function (jqXHR, status, message) { // never called
            // would like this to handle my status code
        alert(status + " " + message);
    }
});

On the local IIS server, the corresponding code that’s being invoked is given as:

[OperationContract]
[WebGet(RequestFormat=WebMessageFormat.Json)]
bool ThisShouldThrowAnException();


public bool ThisShouldThrowAnException()
{
    throw new WebFaultException<String>("Something really bad happened.", HttpStatusCode.InternalServerError);
}

The issue is that when I make this call, the success function is called. jQuery seems to swallow my 500 Internal Server Error and instead gives me a 200 OK. Inspection using Fiddler also reveals that my response status code is a 200 OK. When I call via the browser or if I handcraft a request I receive the appropriate 500 Internal Server Error.

My question is: How can I handle a WebExceptionFault inside of a $.ajax(...) call? As it stands, the $.ajax is completely ignoring any status code that gets passed to it.

I tried using ajaxError and statusCode, but neither worked:

$('#error').ajaxError(function() {
    alert("An error occurred");
});

$.ajax({
    url: BaseUrl + 'ThisShouldThrowAnException',
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    success: function (data) {
        alert("Got data: " + data);
    },
    statusCode: {
        500: function() {
            alert("Got 500 Internal Server Error");
        }
    },
    error: function (jqXHR, status, message) {
        alert(status + " " + message);
    }
});

New Findings

On further analysis, when I inspected the query string that jQuery generates I find that the following returns a 200 OK:

http://localhost/RestService/RestService.svc/ThisShouldThrowAnException?callback=jQuery172003801283869839445_1341495740773&_=1341495740777

// on page:
jQuery172003801283869839445_1341495740773("Something really bad happened.",500);

Meanwhile, the following request returns a 500 Internal Server Error:

http://localhost/RestService/RestService.svc/ThisShouldThrowAnException

// on page:
"Something really bad happened"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:13:16+00:00Added an answer on June 7, 2026 at 5:13 am

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

    Under the section Data Types, they mention

    The jsonp type appends a query string parameter of callback=? to the
    URL. The server should prepend the JSON data with the callback name to
    form a valid JSONP response. We can specify a parameter name other
    than callback with the jsonp option to $.ajax().

    When data is retrieved from remote servers (which is only possible
    using the script or jsonp data types), the error callbacks and global
    events will never be fired.

    Which is why your error callback is not firing and its directly going to the success callback.

    EDIT: What does the data look like in the success function? Cuz from what I understand, whatever is returned from the server is passed in as data.

    Here is an example of consuming a WCF webservice using JSONP.

    http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/

    The key point is, WCF sees that you are requesting JSONP data and that you have provided a query string parameter called ‘callback’. Thus WCF automatically returns a 200 OK (irrespective of actual status) and whatever data was returned by the remote function.

    Further reading on how JSONP actually works:
    http://devlog.info/2010/03/10/cross-domain-ajax/

    If you read how JSONP works, you will realize that’s its not quite possible for jQuery to return the actual error status, as the server itself doesnt return it.

    With regards to your current problem, either switch to JSON requests. If that’s not possible, you can always check the data object in ajax success function. It will have the error code 500 as you are returning it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery code that runs fine until I add the jQuery UI
I have some jquery code that is doing an ajax lookup and returning comma
I have some jQuery code that highlights a link when clicked, and changes the
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some jQuery code that is having difficulty running because I'm using it
I have created some JQuery that will expand a div 'popup' on hover and
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have some DIVs that I am using JQuery to hide and show using
I have some content that I am loading using jquery ajax. The content has
I have a script on jquery that load some site in iframe using: $(#demo_frame).attr('src',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.