gpt4 book ai didi

javascript - 使用dojo/notify api修改Ajax post请求数据

转载 作者:行者123 更新时间:2023-12-03 12:25:53 25 4
gpt4 key购买 nike

我正在尝试使用 DOJO/NOTIFY 将表单数据变量附加到 Ajax Post 请求。例如:有一个表单将项目a = 10发送到服务器。 使用dojo/notify(发送)我在ajax post请求中附加了另一个变量b=5但问题是它不起作用并且只有a=10被发送到服务器。以下是我的代码:

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=10 发送 POST 请求,另请注意,我不再使用 dojo 发送请求/request 模块,但使用 dojo/request/registry 模块(我使用的是 Registry.post())。

然后,该模块将寻找合适的提供程序,在本例中,我们注册了一个始终返回 true 的提供程序,这意味着它将始终被使用。

在该提供程序中,我们正在更改 options.data 对象并向其添加一个额外的属性。 Registry.register() 函数中的其他属性是:

  • Request:告诉提供者系统我们要使用dojo/request模块来处理调用
  • true:告诉您该提供程序应用作第一个提供程序(它具有更高的优先级)

因此,实际上我们向请求添加了一个额外的抽象层,使得拦截调用成为可能。

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

关于javascript - 使用dojo/notify api修改Ajax post请求数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24212637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com