2017-07-06 96 views
2

我創建了一個save(id)函數,它將提交ajax發佈請求。當撥打save(id)。在進入下一步之前,如何從save(id)獲取價值/數據。如何解決這個問題?jQuery Ajax通過函數獲取值?

例如:

 function save(id) { 
      $.ajax({ 
       type: "POST", 
       url: "/post/", 
       dataType: "json", 
       contentType: 'application/json', 
       data: JSON.stringify({ 
        id: id, 
       }), 
       success: function (data) { 
       return data; 
       }, 
       error: function (error) { 
       return data; 
       } 
      }); 
     } 

用法:

$('.btn-create').click(function() { 
    var id = 123; 
    data = saveArea(id); //get data from ajax request or error data? 
    if (data) { 
    window.location = "/post/" + data.something 
    } 
} 
+1

爲什麼不把重定向放在succes回調中? – Ionut

+2

您可以讓您的AJAX調用同步,但請注意數據檢索中的任何放緩操作都會鎖定您的頁面。 –

+1

在成功字段內添加'window.location = ..' – Abra001

回答

4

你有兩個選擇,要麼同步運行AJAX調用(不推薦)。或異步使用回調

同步

由於@Drew_Kennedy提到,直到它完成,這將凍結頁面,降低用戶體驗。

function save(id) { 
    return $.ajax({ 
     type: "POST", 
     url: "/post/", 
     dataType: "json", 
     contentType: 'application/json', 
     async: false, 
     data: JSON.stringify({ 
      id: id, 
     }) 
    }).responseText; 
} 

$('.btn-create').click(function() { 
    var id = 123; 
    // now this will work 
    data = save(id); 
    if (data) { 
    window.location = "/post/" + data.something 
    } 
} 

異步(推薦)

這會在後臺運行,並允許在頁面上正常的使用者互動。

function save(id, cb, err) { 
    $.ajax({ 
     type: "POST", 
     url: "/post/", 
     dataType: "json", 
     contentType: 'application/json', 
     data: JSON.stringify({ 
      id: id, 
     }), 
     success: function (data) { 
      cb(data); 
     }, 
     error: err // you can do the same for success/cb: "success: cb" 
    }); 
} 

$('.btn-create').click(function() { 
    var id = 123; 
    save(id, 
    // what to do on success 
    function(data) { 
     // data is available here in the callback 
     if (data) { 
      window.location = "/post/" + data.something 
     } 
    }, 
    // what to do on failure 
    function(data) { 
     alert(data); 
    } 
    }); 
} 
+1

does'' save'函數需要調用錯誤?不應該是'saveArea(id,function(data){....},function(data){alert'fail');}' – Abra001

+0

這是可選的,'如果你沒有指定它,'JavaScript'不會抱怨直到「AJAX」調用實際上失敗。爲了清楚起見,我會添加它,謝謝! – shotor

+0

沒問題,這是一個新的信息,謝謝你! – Abra001

0

只是讓事情更簡單一點。

對於初學者只需添加window.location = "/post/" + data.something成功回調。 像這樣:

function save(id) { 
    return $.ajax({ 
     type: "POST", 
     url: "/post/", 
     dataType: "json", 
     contentType: 'application/json', 
     data: JSON.stringify({ 
      id: id, 
     }), 
     success:function(data){ 
      window.location = "/post/" + data.something 
     } 
    }).responseText; 
} 

,或者是附加的單擊事件中所有Ajax代碼。

$('.btn-create').click(function() { 
    var id = "123"; 

    $.ajax({ 
    type: "POST", 
    url: "/post/", 
    dataType: "json", 
    contentType: 'application/json', 
    data: JSON.stringify({ 
     id: id, 
    }), 
    success: function (data) { 
    window.location = "/post/" + data.something 
    }, 
    error: function (error) { 
     console.log(error) 
    } 
    }); 

}