0

Within my code I want to catch some POST request errors. To test this, I want to fake the response of the POST request. Is there a way I can do this in JavaScript?

$.ajax({
    url: url,
    type: 'POST',
    // further arguments
}).done(function (result) {
    // Sunny day
}).fail(function(result) {
    // Catch it!
});

I want a statuscode to be returned other than 200 as a response

Ivar
  • 6,138
  • 12
  • 49
  • 61
Klyner
  • 3,983
  • 4
  • 26
  • 50
  • I want a statuscode returned as a response other than 200 . It is within my Javascript code. I'm sorry for the confusion. – Klyner Jan 24 '18 at 09:29

3 Answers3

2

From the documentary page of jQuery (Source);

jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});

An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

so if you end your response of your url with a http code other than 200 will end in '.fail'.

For example if you add a header to your php file like below it should work.

header("HTTP/1.0 404 Not Found");

An alternative method to the header() function is http_response_code() you may check that link http_response_code()

Hope this helps

Cihan Uygun
  • 2,128
  • 1
  • 16
  • 26
  • I am trying to add the request header, but I am getting the following response: "SyntaxError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'HTTP/1.0 404 Not Found' is not a valid HTTP header field name." Instead I rather get a 404 Not Found sothat I can catch this. Should I convert this into a 'header' and 'value' instead of adding the string completely? – Klyner Jan 24 '18 at 08:53
  • Actually you do not need to use `setRequestHeader` why u try to do this? Since you try to check `.done` or `.fail` callbacks we have a simple rule separation of these 2 callbacks. If your resource responses with 200 code which means OK in http messaging will be handled in `.done` all others will be handled in `.fail`. So if you 'fake' your response with the code other than 200 your `.fail` jQuery callback will fire. Conclusion: you should make header modifiction in your php file, not in javascript code. – Cihan Uygun Jan 24 '18 at 08:59
  • Also i have updated my answer with alternative method for header(); you may check the changes. – Cihan Uygun Jan 24 '18 at 09:03
  • That's stupid of me... The question is related to my javascript file. I'm sorry for the confusion. But still. I tried to make a header modification within `setRequestHeader`. Should I do this somewhere else? Thanks for your help. – Klyner Jan 24 '18 at 09:06
  • 1
    You are welcome. So you may try the answer in that question https://stackoverflow.com/questions/21110775/can-i-force-jquery-deferred-ajax-to-execute-fail-handler-after-it-has-been-resol – Cihan Uygun Jan 24 '18 at 09:38
1

The most simple way could be to use just call an url that actually returns that code like so:

$.ajax({
    url: 'https://httpstat.us/400',
    type: 'POST',
    // further arguments
}).done(function (result) {
    console.log("Success");
}).fail(function(result) {
    console.log("Fail");
    console.log("Result", result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

But you can also, based on this answer, override the $.ajax() function to always fail like so:

function ajax_fail(jqXHR, textStatus, errorThrown) {
    return function (params) {
        var deferred = $.Deferred().reject(jqXHR, textStatus, errorThrown);
        return deferred.promise();
    }
}

$.ajax = ajax_fail();

$.ajax({
    url: '',
    type: 'POST'
}).done(function (result) {
    console.log("Success");
}).fail(function (result) {
    console.log("Error");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The parameters you pass to the ajax_fail() function will be the ones your .fail() callback will add. You can add there whatever you like to be returned.

Small warning: Don't use this in production or if you still want other $.ajax calls to work. Every time you call $.ajax() it will fail this way.

If you want to only do this for one AJAX call, do something like this:

var ajaxFunc = $.ajax;
// use the above code

// restore old $.ajax function
$.ajax = ajaxFunc;
Ivar
  • 6,138
  • 12
  • 49
  • 61
1

Better to use fiddler autoResponder than js. You can catch response with 200 code, edit the response and test your error handling.

Stanislav Mayorov
  • 4,298
  • 5
  • 21
  • 44