与同步ajax调用返回值混淆

时间:2021-04-25 09:51:48

I'm having some issues with returning a value from a synchronous ajax call. The value I want to return is a class I created for a server response.

我在从同步ajax调用返回值时遇到一些问题。我想要返回的值是我为服务器响应创建的类。

Here's the AJAX code:

这是AJAX代码:

function webRequest(file, data) {
    return $.ajax({
        url: "http://xxx.xx.xx.xxxx/xxxxx/"+file,
        type: "POST",
        data: data,
        asynch: false,
        error: function(jqXHR, textStatus, errorThrown){
            return new ServerResponse(false, errorThrown);
        },
        success: function(data, textStatus,  jqXHR){
            return new ServerResponse(true, data);
        },
        timeout: 7500
    });    
}

Here's ServerResponse.js

var success = false;
var text = null;

var ServerResponse = function(success, text) {
    this.success = success;
    this.text = text || null;
};

ServerResponse.prototype.isSuccessful = function() {
    return this.success;  
};

ServerResponse.prototype.getData = function() {
    return this.text;
};

The returned value of webRequest(..) is as follows:

webRequest(..)的返回值如下:

Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}abort: function ( statusText ) {always: function () {complete: function () {done: function () {error: function () {fail: function () {getAllResponseHeaders: function () {getResponseHeader: function ( key ) {overrideMimeType: function ( type ) {pipe: function ( /* fnDone, fnFail, fnProgress */ ) {progress: function () {promise: function ( obj ) {readyState: 0responseText: ""setRequestHeader: function ( name, value ) {state: function () {status: 0statusCode: function ( map ) {statusText: "error"success: function () {then: function ( /* fnDone, fnFail, fnProgress */ ) {__proto__: Object VM2324 controllers.js:48

How can I return the ServerResponse instance created from within the ajax call?

如何返回从ajax调用中创建的ServerResponse实例?

1 个解决方案

#1


3  

@fuyushimoya's answer is almost there, just return the newly instantiated server response object from the wrapper function.

@ fuyushimoya的答案几乎就在那里,只是从包装函数返回新实例化的服务器响应对象。

function webRequest(file, data) {
    var serverResponse;
    $.ajax({
        url: "http://xxx.xx.xx.xxxx/xxxxx/"+file,
        type: "POST",
        data: data,
        async: false,
        error: function(jqXHR, textStatus, errorThrown){
            serverResponse = new ServerResponse(false, errorThrown);
        },
        success: function(data, textStatus,  jqXHR){
            serverResponse = new ServerResponse(true, data);
        },
        timeout: 7500
    });
    return serverResponse;
}

This way you can do

这样你就可以做到

var sr = webRequest('some/file', someData);

#1


3  

@fuyushimoya's answer is almost there, just return the newly instantiated server response object from the wrapper function.

@ fuyushimoya的答案几乎就在那里,只是从包装函数返回新实例化的服务器响应对象。

function webRequest(file, data) {
    var serverResponse;
    $.ajax({
        url: "http://xxx.xx.xx.xxxx/xxxxx/"+file,
        type: "POST",
        data: data,
        async: false,
        error: function(jqXHR, textStatus, errorThrown){
            serverResponse = new ServerResponse(false, errorThrown);
        },
        success: function(data, textStatus,  jqXHR){
            serverResponse = new ServerResponse(true, data);
        },
        timeout: 7500
    });
    return serverResponse;
}

This way you can do

这样你就可以做到

var sr = webRequest('some/file', someData);