简体   繁体   中英

Send data to WebAPI (C#) GET Request Ajax call

I want to use html to get WebApi ReturnValue. (type:Get、datatype:Jsonp)

Now when I direct execution WebApi, it can get ReturnValue "2" (indicate execution Success).

But when via by html + Jquery + ajax , it always show error function information. 在此处输入图片说明

How an I resolve this problem, thanks.

------------- code -------------

API Model:

 public class LoginModel
    {
        public int Userno { get; set; }
        public List<Second> LoginSecond { get; set; }
    }
    public class Second
    {
        public string testinfo01 { get; set; }
        public string testinfo02 { get; set; }
        public List<Third> Third { get; set; }
    }

    public class Third
    {
        public string finValue { get; set; }
    }

API Controller:

 [HttpGet]
    public string Get(string id, string id1) //id=Account,id1=password
    {
        string conn = ConfigurationManager.ConnectionStrings["testconn"].ConnectionString;
        string Dictionary = Login.Login(id, id1).ToString();
        return Dictionary;
    }

Html:

<div class="form" id="form"> 
    <form class="login-form" method="get">
    <input type="text" id="inpAcc" placeholder="Acc"/>
    <input type="password" id="inpPwd" placeholder="pwd"/>
    <input id="loginbtn" type="button" value="login"></input>
    <input id="data" type="label" />
  </form>
</div>

jquery + ajax:

<script type="text/javascript" >
    $(document).ready(function() {
          $(document).on('click', '#loginbtn',function(){
                var username = $("input#inpAcc").val();
                var password = $("input#inpPwd").val(); 
                alert(username);
                var APIurl = "localhost:10733/Api/Login/";
              $.ajax({
                  type: "get",
                  dataType: 'jsonp',
                  username:username,
                  password:password,
                  url: APIurl +"/"+username+"/"+password,
                  async: false,
                  headers: { "cache-control": "no-cache" },
                  contentType: 'application/json; charset=utf-8',

               data :function (data)
               {
                var returnvalue = data[0].request();
                console.log(data.stat);
                console.log(data.status);
                console.log(data.message);
                console.log(data.html);
                alert(returnvalue);
               },
              error: function(request, status, error) {

                console.log(request.stat);
                console.log(request.status);
                console.log(request.message);
                console.log(request.html);
                alert(request.responseText);
              },
              success: function(data) {
                console.log(data.stat);
                console.log(data.status);
                console.log(data.message);
                console.log(data.html);
                alert(data);
              }
          });        
      });
  });
</script>

Please check your source. Remove the route configuration as below source included route config inline.

[RoutePrefix("api/Login")]
public class LoginController : ApiController
{
    [HttpGet]
    [Route("Get/{id}/{id1?}")]
    public string Get(string id, string id1 = null) //id=Account,id1=password
    {
        return "Working";
    }
}

to call above api your url should be like

http://localhost:11299/api/login/Get/id/id1/

In the above id1 is optional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM