alok_dida
alok_dida

Reputation: 1733

$.ajax making API call twice though it's getting called once only

I am facing weird issue. I am implementing one SPA. I am using MVC, Sammy router, require JS and Knockout here.

Here is a file where I defined function to make a call to the API.

define(['jquery', 'UIBlock'], function ($) {
    var GlobalParameters = function () {
        this.Parameters;
        this.APIEndPoint = 'http://localhost:24774/API/';
    };

    GlobalParameters.prototype.AjaxCallToServer = function (url, requestType, data, successCallback) {
        $.blockUI();
        $.ajax({
            url: this.APIEndPoint + url,
            data: data,
            contentType: "application/json",
            type: requestType,
            statusCode: {
                500: function () {
                    $('#info').html('<p>An error has occurred while processing your request.</p>');
                    $('#info').show();
                },
                409: function (xhr, ajaxOptions, thrownError) {
                    var message = JSON.parse(xhr.responseText).ExceptionMessage;
                    $('#info').html(message);
                    $('#info').show();
                },
                204: function (data) {
                    $('#info').html('<p>No data found.</p>');
                    $('#info').show();
                }
            },
            dataType: 'json',
            success: successCallback,
            complete: function () {
                $.unblockUI();
                setTimeout(function () {
                    $('#info').hide();
                    $('#info').html("");
                }, 3000);
            }
        });
    };

    return {
        GlobalParameters: GlobalParameters
    }
});

I added debugger and found that it's getting called once only.

Here is the network trace from the Google chrome developer tool. enter image description here

Here is the details of each request.

enter image description here enter image description here

Upvotes: 3

Views: 2519

Answers (1)

Sanjay Sahani
Sanjay Sahani

Reputation: 580

It's normal behavior its called preflighted request. Unlike simple requests "preflighted" requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send

see this for more detail.

Upvotes: 2

Related Questions