将身体的请求发布到模型中(Post request with body into a model)

编程入门 行业动态 更新时间:2024-10-23 16:26:27
将身体的请求发布到模型中(Post request with body into a model)

我必须使用javascript实现它。

我有这堂课:

Public Class RecommendedJobsData Public Property searchedJobsList As List(Of JobInfo) Public Property jobFromStorage As JobInfo Public Property totalJobsCount As Integer Public Property adType As AdType Public Sub New() ' default constructor End Sub ' constructor with parameters Public Sub New(searchedJobsList As List(Of JobInfo), jobFromStorage As JobInfo, totalJobsCount As Integer, adType As AdType) Me.searchedJobsList = searchedJobsList Me.jobFromStorage = jobFromStorage Me.totalJobsCount = totalJobsCount Me.adType = adType End Sub End Class

我正在尝试向此函数发送一个帖子请求:

<JsonpFilter()> Public Function GetToken(header As RequestHeader, body As RecommendedJobsData) As String Return String.Empty End Function

所以我使用了javascript:

var http = new XMLHttpRequest(); http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.onreadystatechange = function () {//Call a function when the state changes. if (http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(apiParams);

标题传递正常,但身体不是..

它进入默认构造函数而不是获取参数的构造函数,然后, GetToken函数的body对象中有null值。

这是apiParams:

任何帮助赞赏!

I had to implement it using javascript.

I have this class:

Public Class RecommendedJobsData Public Property searchedJobsList As List(Of JobInfo) Public Property jobFromStorage As JobInfo Public Property totalJobsCount As Integer Public Property adType As AdType Public Sub New() ' default constructor End Sub ' constructor with parameters Public Sub New(searchedJobsList As List(Of JobInfo), jobFromStorage As JobInfo, totalJobsCount As Integer, adType As AdType) Me.searchedJobsList = searchedJobsList Me.jobFromStorage = jobFromStorage Me.totalJobsCount = totalJobsCount Me.adType = adType End Sub End Class

I am trying to send a post request to this function:

<JsonpFilter()> Public Function GetToken(header As RequestHeader, body As RecommendedJobsData) As String Return String.Empty End Function

So I used javascript:

var http = new XMLHttpRequest(); http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.onreadystatechange = function () {//Call a function when the state changes. if (http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(apiParams);

The header is passed fine, but the body is not..

It enters the default constructor instead of the constructor that gets parameters, and then, there are null values in the body object of GetToken function.

This is apiParams:

Any help appreciated!

最满意答案

我已将内容类型更改为:

http.setRequestHeader("Content-type", "application/json");

然后我改变了发送功能:

http.send(JSON.stringify(apiParams));

最重要的是:

GetToken函数中的<FromBody()>属性:

Public Function GetToken(header As RequestHeader, <FromBody()> body As RecommendedJobsData)

请注意添加对system.web.http的引用。

I have changed the content-type to be:

http.setRequestHeader("Content-type", "application/json");

And then I changed the send function:

http.send(JSON.stringify(apiParams));

And the most important thing is:

<FromBody()> attribute in GetToken function:

Public Function GetToken(header As RequestHeader, <FromBody()> body As RecommendedJobsData)

Note to add a reference to system.web.http.

更多推荐

本文发布于:2023-08-07 17:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   身体   Post   model   body

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!