0

I have an application that fires an ajax jsonp request to a C# HttpHandler.

function RequestData() {
var parameters = 'value1=' + value + '&value2=' + value2;
$.ajax({
    type: "GET",
    url: 'https://localhost:44300/checkvalues?' + parameters,
    dataType: "jsonp",
    headers: { "cache-control": "no-cache" },
    success: function (msg) {
        alert('all good')
    },
    error: function (jqXHR, exception) {
        alert(jqXHR.status);
    }
});

And here is some of the server side code.

if (OK)
{
    response.ContentEncoding = System.Text.Encoding.UTF8;
    response.ContentType = "application/javascript";
    response.Write(callback + "({ data: 'allOK' });");
}
else
{
    //error
    response.StatusCode = 500;
    response.SuppressFormsAuthenticationRedirect = true;
    response.StatusDescription = "error";
    response.End();
}

When OK is true, there is no problem. The ajax success function is called as expected. But the minute that I set the response status code to e.g. 500 to trigger the error section of the ajax request, the server response is never received - nothing happens.

How can I modify my response code to enter the ajax error section?

I can trigger a parse-error by changing the response, but I want to do it with Http Status Codes.

Mr. Blonde
  • 711
  • 2
  • 12
  • 27
  • Why don't use send statusCode in JSON instead. – jcubic Feb 22 '16 at 14:29
  • Look at the request in the webinspector (F12). What is the response status code? I would expect your `error` function to trigger. – Halcyon Feb 22 '16 at 14:30
  • @jcubic - It seems to me, that I would have to handle the error in the "success" section. It seems more logical to handle it in the error section - if possible. – Mr. Blonde Feb 22 '16 at 14:33
  • `jsonp` requests are script requests , not XMLHtttpRequests. Error handling capabilities are different. See docs : `This handler is not called for cross-domain script and cross-domain JSONP requests` – charlietfl Feb 22 '16 at 14:41
  • @Halcyon The request fired is [link](https://localhost:44300/checkvalues?callback=jQuery18006971448138356209_1456151615496&value1=value&value2=value2&_=1456151695981) - if I copy that URL into a browser, the server responds correctly with an Error 500. So why is it not detected by ajax? – Mr. Blonde Feb 22 '16 at 14:41
  • @Mr.Blonde it should. Do you see the request in the webinspector? – Halcyon Feb 22 '16 at 14:42
  • 1
    Look at the answers provided in the duplicate I linked. Error doesn't get called for cross domain jsonp requests [ajax](http://api.jquery.com/jQuery.ajax/) `Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.` – wirey00 Feb 22 '16 at 14:44
  • @Halcyon: Yes, I see the request. And it is marked with a red X in the inspector. – Mr. Blonde Feb 22 '16 at 14:44
  • 1
    Suggest you implement CORS instead of jsonp – charlietfl Feb 22 '16 at 14:45
  • @charlietfl That is correct. jQuery documentation states that "Error" is not called when using jsonp. – Mr. Blonde Feb 22 '16 at 14:48
  • in your HttpHandler, should indicate that it is json... response.ContentType = "application/json; charset=utf-8"; – CMedina Feb 22 '16 at 14:50
  • jQuery is being silly, you can detect an error, I don't know why jQuery chooses not to. You can write your own JSONp request implementation (it's actually fairly simple). To detect an error add a listener to `onerror` of the ` – Halcyon Feb 22 '16 at 14:52

1 Answers1

1

You can detect a JSONp error. I'm not sure why jQuery chooses not to.

Here is a JSONp implementation that doesn't use jQuery. You might need to tinker with it a bit to make it work. For instance I'm not sure how jQuery communicates the callback_name.

function jsonp(success_callback, error_callback) {
    var script, callback_name;
    var parameters = 'value1=' + value + '&value2=' + value2;

    callback_name = "generate random name";

    function after() {
        setTimeout(function () {
            document.getElementsByTagName("head")[0].removeChild(script);
        }, 1);
    }

    script = document.createElement('script');
    window[callback_name] = function (response) {
        after();
        success_callback(response);
    };
    script.type = 'text/javascript';
    script.src = "https://localhost:44300/checkvalues?" + parameters + "&callback=" + callback_name;
    script.async = true;
    script.addEventListener('error', function () {
        after();
        error_callback();
    });
    document.getElementsByTagName("head")[0].appendChild(script);
}

jsonp(function () {
    alert("success");
}, function () {
    alert("failure");
});
Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • 1
    Marked as correct - it works. I will however use the jQuery plugin as suggested in one of the dublicate links added by @ᾠῗᵲᄐᶌ : https://github.com/jaubourg/jquery-jsonp - although I didn't find the http status code in that solution. – Mr. Blonde Feb 22 '16 at 16:20