使用表单如何将POST的有效负载作为JSON发送

时间:2022-12-01 15:59:01

I am using HTML to download xlsx,pdf files using POST call. I am using the below code to send my payload but it is sending as url format,

我正在使用HTML下载xlsx,pdf文件使用POST call。我用下面的代码发送有效负载但它是以url格式发送的,

 <form method="post" action="{{url}}" enctype='application/json'>
<input type="text" name="type" value="{{type}}" hidden>
<input type="text" name="paramValue" value="{{value}}" hidden>
</form>


Request body
type=Shop&paramValue=Kumar

I need to pass it as JSON because of this I am getting an error as,

我需要将它作为JSON传递因为我得到了一个错误,

Response    HTTP/1.1 415 Unsupported Media Type

Please suggest how to pass the data as JSON in case of using form in HTML.

请建议如何将数据作为JSON传递,以在HTML中使用表单。

1 个解决方案

#1


1  

Form enctype attribute does not support application/json as seen here.

Form enctype属性不支持这里看到的application/json。

You need to manually json encode your data to use with your post request or use the $http service which posts json by default:

您需要手动对数据进行json编码,以便与post请求一起使用,或者使用默认发布json的$http服务:

var data = { type: $scope.form.type, value: $scope.form.value };

$http.post(url, data) // default content-type is 'application/json' for $http.post
.success(function(data, status) {
  //yay
});

#1


1  

Form enctype attribute does not support application/json as seen here.

Form enctype属性不支持这里看到的application/json。

You need to manually json encode your data to use with your post request or use the $http service which posts json by default:

您需要手动对数据进行json编码,以便与post请求一起使用,或者使用默认发布json的$http服务:

var data = { type: $scope.form.type, value: $scope.form.value };

$http.post(url, data) // default content-type is 'application/json' for $http.post
.success(function(data, status) {
  //yay
});