jQuery 如何在有效负载而不是表单数据上发布数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27172974/
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-08-26 11:21:57  来源:igfitidea点击:

How to POST data on payload instead of formdata

jqueryajaxjsonalfrescopayload

提问by Enrico

I'm trying to make a request to an Alfresco service from a web-script I made, passing some json data on the payload.

我正在尝试从我制作的 Web 脚本向 Alfresco 服务发出请求,并在有效负载上传递一些 json 数据。

This is the Alfresco service:

这是露天服务:

http://localhost:8080/share/proxy/alfresco/api/internal/downloads

And I need to pass a json array whit some script node, like that:

我需要传递一个带有一些脚本节点的 json 数组,如下所示:

var jsonData = "[{'nodeRef':'workspace://SpacesStore/920b43d4-e79c-40eb-96f3-1dff3a169929'},  {'nodeRef':'workspace://SpacesStore/f19fba4b-0cf6-4379-a858-70d0d7d9efb0'},{'nodeRef':'workspace://SpacesStore/6ea51288-9364-4070-a23b-499025a6c1f9'}]";

I make the call on this way

我就这样打电话

$.ajax({
    url: serviceUrl,
    type: "POST",
    dataType: "json",
    data: jsonData
}); 

Unfortunately when I chek the request list from the developer tools I see that my json data are passed as Form data on the request and I get an internal server error response.

不幸的是,当我从开发人员工具中查看请求列表时,我看到我的 json 数据作为请求的表单数据传递,并且我收到了内部服务器错误响应。

I saw the same service used on another website and there the data are passed as payload, so, I think really need the data to be passed on the payload.

我在另一个网站上看到了相同的服务,并且数据作为有效负载传递,因此,我认为确实需要在有效负载上传递数据。

Does anyone know how to force it ?

有谁知道如何强制它?

回答by GarethL

I think it depends on the Content-Type header of the request; if the content type is "application/x-www-form-urlencoded" then it is shown under form data. If you put - for example - Content-Type: application/json the json should be part of the payload. You can use:

我认为这取决于请求的 Content-Type 标头;如果内容类型是“application/x-www-form-urlencoded”,那么它会显示在表单数据下。如果你放 - 例如 - Content-Type: application/json json 应该是有效负载的一部分。您可以使用:

$.ajax({
    url: serviceUrl,
    type: "POST",
    dataType: "json",
    data: jsonData,
    contentType: "application/json"
});