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);
}