无法将带有jquery的参数从正文传递到webapi post方法

阿里·斯里蒂

我有一个可以正常工作的webapi(已通过rest client测试)。我正在尝试使用jQuery调用post方法。

$.ajax$.post方法一起发送的参数为空。

表单主体中传递的数据模型为:

public class SearchCandidatCriteria
{
    public string Nom;
    public string Sexe;
    public string Ville;
    public int SituationFamiliale;
    public int NiveauAcademique;
    public int ServiceNational;
    public string PosteDemande;
}

jQuery方法是:

$.ajax({
            'url': 'http://localhost:6232/api/Candidats/rechercher',
            'method': 'POST',
            'data': JSON.stringify('{"Nom":"","Sexe":"F","Ville":"","SituationFamiliale":0,"NiveauAcademique":";n,n,;n,;n","ServiceNational":"2","PosteDemande":"120"}'),
            'Content-Type': 'application/json',
            success : function(data) {
                console.log(data);
            }
        });

控制器接收的参数始终为空

恩科西

您构造的请求不正确

构建JavaScript对象,然后对其进行字符串化

var data = {Nom:"",Sexe:"F",Ville:"",SituationFamiliale:0,NiveauAcademique:";n,n,;n,;n",ServiceNational:"2",PosteDemande:"120"};
$.ajax({
    url: 'http://localhost:6232/api/Candidats/rechercher',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(data),
    contentType: 'application/json',
    success : function(data) {
        console.log(data);
    }
});

该模型还应该使用属性而不是字段

public class SearchCandidatCriteria {
    public string Nom { get; set; }
    public string Sexe { get; set; }
    public string Ville { get; set; }
    public int SituationFamiliale { get; set; }
    public int NiveauAcademique { get; set; }
    public int ServiceNational { get; set; }
    public string PosteDemande { get; set; }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将带有字符串参数的 Action 传递到没有它的方法中

将带有参数的方法传递给包装器组件

带有 url 且没有正文参数的 Angular http post 方法请求

带有POST方法和参数的BackgroundTransferService

带有 Centrasite 查询参数的 POST 方法

无法发送带有正文的 POST

将带有自定义实现方法的通用枚举作为参数传递

如何将带有参数的方法传递给线程以执行?

如何将带有“&”的字典参数传递给 Swift 中的 Objective-C 方法

无法将带有 GM_xmlhttpRequest 的数据从带有 POST 方法的 Tampermonkey 发送到 Scalatra

如何将带有onClick的androidx数据绑定到带参数的静态方法

jQuery post方法的问题

WebApi post方法中的参数绑定

REST WEBAPI POST方法的参数未调用

将JSON字符串作为输入传递给带有Selenium的RESTAssured中POST请求正文的参数之一

将带有标头和正文请求的 HTTP POST 转换为 Python(Telerik Fiddle)

WebApi路由-许多POST方法

正确压缩webapi POST的方法

无法将带有二进制文件的POST发布到Node.js Express服务器

数组作为参数传递以表达post方法

Django DRP post方法如何传递参数

如何使用post方法上传带有参数android的图像

通过方法 post 发出带有参数的 HttpsURLConnection 请求

将带有参数的c#回调方法传递给c ++ dll会导致System.ExecutionEngineException

将带有可选模板类型参数的函数传递给类构造函数,并将其分配给方法

为什么我们不能将带有其他参数的{}块传递给Ruby中的方法

如何将带有事件绑定的hml作为字符串传递到render()方法中?

无法使用POST方法传递表单值

检查POST参数的更好方法?