如何使用web-api传递多个参数

时间:2022-12-27 02:14:08

I want to pass two params using json in web-api, but I get null each time I try, what am I missing ? is there a better way to pass more than one parameter?

我想在web-api中使用json传递两个参数,但每次尝试都会得到null,我错过了什么?是否有更好的方法来传递多个参数?

//HTML

var uri = "api/Login";

//after I click a button this function fires

//单击按钮后,此功能将触发

function checkUser() {
        var email = "ttt@ggg.com";   
       var password = "itsme";
          var details = "{ email:'"+ email+"', pass:'"+ password+"'}";           
        $.ajax({
            type: "Get",          
            data: JSON.stringify(details),
            url: uri,
            contentType: "application/json"
        });
    }

// LoginController

[HttpGet]
public HttpResponseMessage Get([FromBody]string  data)
{
     HttpResponseMessage msg = null;
     //the code run this function, but the 'data' is null
     string userinfo = data;                    
     return msg;
}

2 个解决方案

#1


0  

[FromBody] attribute won't work on GET requests because there is no body in a GET request.

[FromBody]属性不适用于GET请求,因为GET请求中没有正文。

You must either pass the values as parameters or change the method to [HttpPost]. I suggest you change the method to [HttpPost] and change your ajax request method to POST.

您必须将值作为参数传递或将方法更改为[HttpPost]。我建议你将方法更改为[HttpPost]并将你的ajax请求方法更改为POST。

[HttpPost]
public HttpResponseMessage Get([FromBody]string  data)
{
    HttpResponseMessage msg = null;
    //the code run this function, but the 'data' is null
    string userinfo = data;                    
    return msg;
}

and in your ajax request

并在您的ajax请求中

function checkUser() {
    var email = "ttt@ggg.com";   
    var password = "itsme";
      var details = "{ email:'"+ email+"', pass:'"+ password+"'}";           
    $.ajax({
        type: "POST",          
        data: JSON.stringify(details),
        url: uri,
        contentType: "application/json"
    });
}

#2


1  

In addition to GET vs. POST issue noted by @su8898, you are already building a string in details and then trying to stringify it. You should define details as object literal like this:

除了@ su8898注意到的GET与POST问题之外,您已经在构建详细信息字符串,然后尝试对其进行字符串化。您应该将详细信息定义为对象文字,如下所示:

var details = {
    'email': email,
    'pass': password
};

This would give you an actual object to stringify.

这将为您提供stringify的实际对象。

#1


0  

[FromBody] attribute won't work on GET requests because there is no body in a GET request.

[FromBody]属性不适用于GET请求,因为GET请求中没有正文。

You must either pass the values as parameters or change the method to [HttpPost]. I suggest you change the method to [HttpPost] and change your ajax request method to POST.

您必须将值作为参数传递或将方法更改为[HttpPost]。我建议你将方法更改为[HttpPost]并将你的ajax请求方法更改为POST。

[HttpPost]
public HttpResponseMessage Get([FromBody]string  data)
{
    HttpResponseMessage msg = null;
    //the code run this function, but the 'data' is null
    string userinfo = data;                    
    return msg;
}

and in your ajax request

并在您的ajax请求中

function checkUser() {
    var email = "ttt@ggg.com";   
    var password = "itsme";
      var details = "{ email:'"+ email+"', pass:'"+ password+"'}";           
    $.ajax({
        type: "POST",          
        data: JSON.stringify(details),
        url: uri,
        contentType: "application/json"
    });
}

#2


1  

In addition to GET vs. POST issue noted by @su8898, you are already building a string in details and then trying to stringify it. You should define details as object literal like this:

除了@ su8898注意到的GET与POST问题之外,您已经在构建详细信息字符串,然后尝试对其进行字符串化。您应该将详细信息定义为对象文字,如下所示:

var details = {
    'email': email,
    'pass': password
};

This would give you an actual object to stringify.

这将为您提供stringify的实际对象。