简体   繁体   English

对MVC 4 Controller动作的同步javascript调用

[英]Synchronous javascript call to MVC 4 Controller action

I'm writing piece of code, and I need some functions to be executed sequential, I used ajax call but it is asynchronous and i faced problems 我正在编写一段代码,并且需要按顺序执行一些功能,我使用了ajax调用,但是它是异步的,因此我遇到了问题

function GetLibraryActivities(libraryName, callback) {
    $.ajax({
        dataType: "json",
        url: "/WorkflowDesigner/GetLibraryActivities/?libraryName=" + libraryName
    }).done(function (data) {
        console.log(data);
        return data;
    });
}

then I tried to work with callback function, but it didn't work too. 然后我尝试使用回调函数,但是也没有用。

function GetLibraryActivities(libraryName, callback) {
    $.ajax({
        'url': "/WorkflowDesigner/GetLibraryActivities/?libraryName=" + libraryName,
        'type': 'GET',
        'success': callback
    });
}
GetLibraryActivities("Petrophysics", function (data) {
    petrophysicsData = data
});

when I try to use petrophysicsData variable under the code it returns unidentified , I need a mean to call a function in synchronous way, any help will be appreciated thanks. 当我尝试在代码下使用petrophysicsData变量返回unidentified时,我需要一种以同步方式调用函数的方式,感谢您的帮助。

Your main problem here is that you are trying to 'return' something from an AJAX callback. 这里的主要问题是您试图从AJAX回调中“返回”某些内容。 You cannot expect an Async AJAX function to return values to the script function that called it, because the function that called it moved on after the AJAX call started. 您不能期望Async AJAX函数将值返回给调用它的脚本函数,因为在AJAX调用开始后,调用它的函数会继续运行。 That is the point of an Async call, it allows the javascript to move on and not have to wait for the server communication to complete. 这就是异步调用的重点,它允许javascript继续前进,而不必等待服务器通信完成。

The best way to handle this is to call all portions of a function in sequential order by using callbacks, never planning to use a return. 解决此问题的最佳方法是通过使用回调按顺序调用函数的所有部分,而不打算使用返回。

So, rather than returning data it would be best to instead call a function that processes data from within the callback function. 因此,与其返回data ,不如调用回调函数中处理data的函数,而不是返回data It can be a little inconvenient, but when working with Async calls, it is best to assume that you can only go deeper into your call stack, rather than returning anything back out of it. 这可能有点不方便,但是在使用异步调用时,最好假设您只能更深入地进入调用堆栈,而不是从中返回任何内容。

So, rather than your first option...you would want to do something like this instead... 因此,除了您的首选之外,您还想做这样的事情……

function GetLibraryActivities(libraryName, callback) {
    $.ajax({
        dataType: "json",
        url: "/WorkflowDesigner/GetLibraryActivities/?libraryName=" + libraryName
    }).done(function (data) {
        console.log(data);
        ProcessResults(data);
    });
}

Or, simply perform your processing within the callback. 或者,只需在回调中执行处理即可。

Pre jQuery 1.8, there was the 'async' option, that would allow you to force Javascript to wait for the ajax call to process...but this locks up the browser while it is processing, and has since been deprecated. 在jQuery 1.8之前的版本中,有一个'async'选项,它可以让您强制Javascript等待ajax调用进行处理...但是这会在处理过程中锁定浏览器,此后不推荐使用。

If you simply return the Ajax promise like this: 如果您只返回如下的Ajax承诺:

function GetLibraryActivities(libraryName, callback) {
    return $.ajax({
        'url': "/WorkflowDesigner/GetLibraryActivities/?libraryName=" + libraryName,
        'type': 'GET'
    });
}

you can do this to use the value: 您可以执行以下操作以使用值:

GetLibraryActivities("Petrophysics").done(function (data) {
    // do something with petrophysics data
});

and chain them "sequentially" with then like this: then像这样“依次”链接它们:

GetLibraryActivities("Petrophysics").then(GetLibraryActivities("Astrophysics")).done(function(pertro, astro){
    // Do something with both results};
});

In this example both will load in parallel, but the done will only be called with both promises have completed (in any order). 在此示例中,两者将并行加载,但只有在两个promise均已完成(以任何顺序)的情况下,才能调用done。

If you have loads of these to load, you can use $.when to process multiple promises. 如果您要加载这些负载,则可以使用$.when处理多个promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM