0

当我将参数发布到服务器时,jQuery 加载的回调函数没有执行。

这会导致服务器错误,但回调函数会执行:

chassisLookupDialog.load('../Workflow/FindChassisDialog/', function (response, status, xhr) {

    console.log("Response", response, status, xhr);

    chassisLookupDialog.open();
});

这行得通,参数到达服务器并且没有抛出错误,但回调函数永远不会执行:

chassisLookupDialog.load('../Workflow/FindChassisDialog/', {
        dataCenterID: dataCenterID,
        bladeTemplateID: bladeTemplateID,
        selectedChassisID: selectedChassisID,
        selectedSlotNumber: selectedSlotNumber
    }, function (response, status, xhr) {

    console.log("Response", response, status, xhr);

    chassisLookupDialog.open();
});

这是我的控制器:

public ActionResult FindChassisDialog(int dataCenterID, int bladeTemplateID, int selectedChassisID, int selectedSlotNumber)
{
    FindChassisDialogModel findChassisDialogModel = new FindChassisDialogModel();

    return View("FindChassisDialog", findChassisDialogModel);
}

如果我检查服务器的响应,我会看到所有 HTML 格式正确。不知道为什么我的回调不会执行......有什么想法吗?

这使用视图的 html 作为错误数据调用错误事件:

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: '../Workflow/FindChassisDialog/',
    data: {
        dataCenterID: dataCenterID,
        bladeTemplateID: bladeTemplateID,
        selectedChassisID: selectedChassisID,
        selectedSlotNumber: selectedSlotNumber
    },
    success: function (data) {
        console.log("Success:", data);
    },
    error: function (error) {
        console.error("Error:", error);
    }
});
4

1 回答 1

1

In your $.ajax request get rid of the following line:

dataType: 'json',

I mean, you are telling jQuery that this AJAX request will return JSON, and yet you are returning HTML from your controller action (return View(...)). So when jQuery attempts to parse this as JSON it obviously bombs out and your success callback will never be executed.

So:

$.ajax({
    type: 'POST',
    url: '../Workflow/FindChassisDialog/',
    data: {
        dataCenterID: dataCenterID,
        bladeTemplateID: bladeTemplateID,
        selectedChassisID: selectedChassisID,
        selectedSlotNumber: selectedSlotNumber
    },
    success: function (data) {
        console.log("Success:", data);
    },
    error: function (error) {
        console.error("Error:", error);
    }
});

By the way, hardcoding an url like that is just plain wrong:

url: '../Workflow/FindChassisDialog/',

You should always be using url helpers in an ASP.NET MVC application when dealing with urls.

And one more thing: why are you returning an entire view (instead of partial) from a controller action that gets invoked wit AJAX?

于 2013-06-27T20:01:28.330 回答