weixin_33713350 2015-08-24 16:58 采纳率: 0%
浏览 123

使用Ajax调用Web Api方法

I wrote a WebApi project that get firstname and lastname as a input parameter and return all the info. I use RestShop to call the webApi and return data. This is working and is returning but for some reason I have to use Ajax call to call the api. I can’t get the Ajax call working. I have used different links in this website but it return me empty data and error. it retuen empty for both alerts: alert(FirstName); and at the end retues [object][object] error.

This is the RestShop that is working:

 public List<Data> GetData(string FirstName, string LastName)
    {
        RestClient _client = new RestClient();
        string _url = "http://localhost:51142";
        _client = new RestClient(_url);


        //DocGen/Documents/GetByFLName/TERRI/TIMMERMAN
        var request = new RestRequest("Gen/Doc/GetByFLName/{FirstName}/{LastName}", Method.GET) { RequestFormat = DataFormat.Json };
        request.AddParameter("FirstName", FirstName, ParameterType.UrlSegment);
        request.AddParameter("LastName", LastName, ParameterType.UrlSegment);

        var response = _client.Execute<List<Data>>(request);
        if (response.Data == null)
            throw new Exception(response.ErrorMessage);

        return response.Data;

    }

This is Ajax code to call same method ans it returns , [object][object] error.

function GetEmployee() {
    jQuery.support.cors = true;
    var FirstName = $('#FirstName').val();
    alert(FirstName);
    var LastName = $('#LastName').val();
    alert(LastName);
    $.ajax({
        url: 'http://localhost:51142/Gen/Doc/GetByFLName/'+ '/' + FirstName + LastName,
        type: 'GET',
        dataType: 'json',
        success: function (data) {
                            alert(data);
        },
        error: function (x, y, z) {
            alert(x + '
' + y + '
' + z);
        }
    });
}

and this is textboxes:

First Name:
   @Html.TextBox("FirstName")

  Last Name:
  @Html.TextBox("LastName")

and this is my method in webApi:

    public IHttpActionResult GetByFLName(string id1, string id2)
    {

        DAL.DataManager dal = new DAL.DataManager();
        CMSIGateway gateway = new CMSIGateway();
        gateway = dal.Get_CM_BY_FLName(id1, id2);


        if (gateway == null)
        {
            return NotFound();
        }

        return Ok(gateway);
    }
  • 写回答

1条回答 默认 最新

  • weixin_33701294 2015-08-25 03:38
    关注

    ur putting incorrect parameter to the url it should be like this

     url: 'http://localhost:51142/Gen/Doc/GetByFLName?id1=' + FirstName + "&id2=" +LastName
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵