json Dojo dojo.rawXhrPost 和 dojo.xhrPost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1846927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:22:15  来源:igfitidea点击:

Dojo dojo.rawXhrPost and dojo.xhrPost

jsondojo

提问by djna

My question is: can we use dojo.xhrPost to post some Json data? More detail:

我的问题是:我们可以使用 dojo.xhrPost 发布一些 Json 数据吗?更多详情:

I have been experimenting with Dojo code to POST JSON data to a RESTful service. It seems to be that the behaviours of dojo.xhrPostand dojo.rawXhrPostare different, or to be more accurate rawXhrPost() works and xhrPost() does not. This is not consistent with my reading of the docs

我一直在尝试使用 Dojo 代码将 JSON 数据发布到 RESTful 服务。似乎是dojo.xhrPostdojo.rawXhrPost的行为不同,或者更准确地说,rawXhrPost() 起作用而 xhrPost() 没有。这与我对文档的阅读不一致

The original purpose of dojo.rawXhrPost was a method that could be used to send a raw post body to the server. As of 1.3, this function is common with dojo.xhrPost(). So, for usage of dojo.rawXhrPost(), see dojo.xhrPost()

dojo.rawXhrPost 的最初目的是一种可用于将原始帖子正文发送到服务器的方法。从 1.3 开始,此函数与 dojo.xhrPost() 通用。因此,有关 dojo.rawXhrPost() 的用法,请参阅 dojo.xhrPost()

Which implies that xhrPost() is enough. My code looks like this - I've got a "toy" library service that manages Editions of Books. The code wants to POST a new entry,

这意味着 xhrPost() 就足够了。我的代码看起来像这样 - 我有一个管理书籍版本的“玩具”图书馆服务。代码想要发布一个新条目,

        var myEdition = {"Edition":{"isbn":"44"}};

        var xhrArgs = {
            url: "http://localhost:8081/LibraryWink/library/editions",
            postData: dojo.toJson(myEdition),
            handleAs: "json",
            headers: { "Content-Type": "application/json"},

            load: function(data) {
                dojo.byId("mainMessageText").innerHTML = "Message posted.";
            },
            error: function(error) {

                dojo.byId("mainMessageText").innerHTML = "Error :" + error;
            }
        };

        var deferred = dojo.rawXhrPost(xhrArgs);

The headers: { "Content-Type": "application/json"}part in necessary so that my JAX-RC service understands that the content is JSON.

头:{“内容类型”:“应用/ JSON”}在必要让自己的JAX-RC服务的理解是,内容是JSON的一部分。

What I find is that the code above works perfectly. However if instead I say:

我发现上面的代码运行完美。但是,如果我说:

var deferred = dojo.xhrPost(xhrArgs);

No data is transmitted in the POST. I have a TCP/IP monitor in place and can see that there is nothing transmitted.

POST 中不传输任何数据。我有一个 TCP/IP 监视器,可以看到没有任何传输。

So, is this a bug, or am I driving xhrPost() incorrectly? Or should I use rawXhrPost()? If the latter, under what circumstances do we use the two flavours of XhrPost?

那么,这是一个错误,还是我错误地驱动了 xhrPost() ?或者我应该使用 rawXhrPost() 吗?如果是后者,我们在什么情况下使用 XhrPost 的两种风格?

回答by lambacck

As of DOJO 1.4 the this should work:

从 DOJO 1.4 开始,这应该可以工作:

var myEdition = {"Edition":{"isbn":"44"}};

var xhrArgs = {
    url: "http://localhost:8081/LibraryWink/library/editions",
    postData: dojo.toJson(myEdition),
    handleAs: "json",
    headers: { "Content-Type": "application/json"},
    load: function(data) {
        dojo.byId("mainMessageText").innerHTML = "Message posted.";
    },
    error: function(error) {

        dojo.byId("mainMessageText").innerHTML = "Error :" + error;
    }
};

dojo.xhrPost(xhrArgs);

If you are posting JSON data, the Content-Type header is critical. If you don't add it, the browser will default to 'application/x-www-form-urlencoded' and URL encode your data for you.

如果您要发布 JSON 数据,则 Content-Type 标头至关重要。如果您不添加它,浏览器将默认为“application/x-www-form-urlencoded”并为您对数据进行 URL 编码。

You may want to add a charset to the Content-Type header (I do this) but that does not stop it from functioning:

您可能想要向 Content-Type 标头添加一个字符集(我这样做),但这并不能阻止它运行:

    headers: { "Content-Type": "application/json; charset=utf-8"}

On Firefox 3.6 at least, the charset is automatically added.

至少在 Firefox 3.6 上,字符集会自动添加。

As Dom mentions, the HTTP PUT equivalent is dojo.xhrPut. The difference here is that you need to add your request body data as putData instead of postData.

正如 Dom 所提到的,HTTP PUT 等效项是 dojo.xhrPut。这里的区别在于您需要将请求正文数据添加为 putData 而不是 postData。

回答by Dom Derrien

While using Dojo library from http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/dojo.xd.js, I've no issue posting data from a form (data serialized by dojo.formToJson()).

在使用来自http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/dojo.xd.js 的Dojo 库时,我从表单发布数据(数据由 序列化dojo.formToJson())没有问题。

dojo.xhrPut({
    putData: dojo.formToJson("locationInformation"),
    handleAs: "json",
    load: function(response, ioArgs) {
        // ... business logic ...
    },
    error: function(message, ioArgs) { alert(message+"\nurl: "+ioArgs.url); },
    url: "/API/Location"
});

Using Firebug in Firefox, I can see that my request is built as expected:

在 Firefox 中使用 Firebug,我可以看到我的请求是按预期构建的:

  • Among other request headers: Content-Type = application/json; charset=UTF-8
  • Body of the Put request: {"postalCode":"h8p3r8","countryCode":"CA"}
  • 在其他请求标头中: Content-Type = application/json; charset=UTF-8
  • Put 请求的正文: {"postalCode":"h8p3r8","countryCode":"CA"}

xhrPost/xhrPut seem to work as rawXhrPost/rawXhrPut...

xhrPost/xhrPut 似乎像 rawXhrPost/rawXhrPut 一样工作...

回答by Richard Ayotte

One more thing that I would like to add the the answer. When working with AJAX apps, it's also a good idea to set the Accept value to application/json when that's what you're expecting.

我想添加答案的另一件事。使用 AJAX 应用程序时,将 Accept 值设置为 application/json 也是您期望的一个好主意。

headers: { "Content-Type": "application/json", "Accept" : "application/json"}