$ .ajax进行两次API调用虽然它只被调用一次

时间:2016-03-02 06:11:38

标签: jquery ajax asp.net-mvc requirejs sammy.js

我面临着奇怪的问题。我正在实施一个SPA。我正在使用MVC,Sammy路由器,需要JS和Knockout。

这是一个文件,我在其中定义了调用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
    }
});

我添加了调试器,发现它只被调用一次。

以下是来自Google Chrome开发人员工具的网络跟踪。 enter image description here

以下是每个请求的详细信息。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

这是正常行为,称为预检请求。与简单请求“预检”请求不同,首先将HTTP OPTIONS请求标头发送到另一个域上的资源,以确定实际请求是否可以安全发送

有关详细信息,请参阅this

相关问题