簡體   English   中英

使用dojo / notify API修改Ajax發布請求數據

[英]Modifying Ajax post request data using dojo/notify api

我正在嘗試使用DOJO / NOTIFY將Form數據變量附加到Ajax Post Request。 例如:有一種形式,它將項a = 10發送到服務器。 使用dojo / notify(發送),我在ajax post請求中附加了另一個變量b = 5但問題是它不起作用,只有a = 10發送到server 以下是我的代碼:

require(["dojo/request", "dojo/request/notify"], function (Request, Notify) {

    //Called before sending Ajax Request
    Notify("send", function (request) {
        dojo.byId("status").innerHTML = "Sending Ajax Request";
        /*
          At this point I want to add another form data item before 
          it is sent to Server.Example b = 5. The following is the 
          way to do it but It does not seem to work:
        */

        request.options.data += "&b=5";
        //I also tries the following but it also not working:
        //request.options.data.b = "5";     
    });

    Request.post("http://jsfiddle.net/",{
        data:{a:10}
    });
});

JSFIDDLE: http : //jsfiddle.net/TEMA2/1/

出於這些目的從來沒有做過dojo/request/notify 您從中獲得的響應僅用於向您提供通知,而未在請求中進行進一步處理,因此實際上將忽略您的修改。

要攔截呼叫,您應該查看dojo/request/registry模塊,該模塊可用於注冊提供程序(可用作攔截程序)。

例如,如果要向請求中添加數據,可以使用:

Registry.register(function(url, options) {
    options.data.b = 5;
    return true;
}, Request, true);

Registry.post("/echo/json",{
    data:{a:10}
});

因此,在這種情況下,我將發送a a=10POST請求,還請注意,我不再使用dojo/request模塊發送dojo/request ,而是通過使用dojo/request/registry模塊發送消息(我正在使用Registry.post() )。

然后,該模塊將尋找合適的提供程序,在這種情況下,我們注冊了一個始終返回true的提供程序,這意味着將始終使用它。

在該提供程序中,我們正在更改options.data對象,並為其添加一個額外的屬性。 Registry.register()函數中的其他屬性是:

  • Request :告知提供者系統我們要使用dojo/request模塊來處理調用
  • true :告訴您應將此提供程序用作第一個提供程序(優先級更高)

因此,實際上我們在請求中添加了一個額外的抽象層,使攔截調用成為可能。

我還更新了您的JSFiddle: http : //jsfiddle.net/TEMA2/2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM