I am trying to create cross domain web api, however I am still unable to call data from web api url, in my ajax function. I am currently experiencing the following syntax error, under the networks, response tag. **SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data**
The ’/ api/stw’ is currently being reported as 405 error, however when I call - http://apitesting.domain.com/api/stw, I am able to see the data is JSON format. So, I am little unsure, why is it failing in the ajax function.
$(document).ready(function () {
$.support.cors = true;
$.ajax({
type: "GET",
crossDomain: true,
contentType: "application/json; charset=utf-8",
// url: "/api/stw",
url: "http://apitesting.domain.com/api/stw,",
data: "{}",
dataType: "json",
success: function (data) {
console.log(data);
// var myData = JSON.parse(data)
for (var i = 0; i < data.length; i++) {
$("#tbDetails").append("" + data[i].Name + "" + data[i].loan + "" + data[i].evnt + "");
}
},
error: function (result) {
alert("Error");
}
});
});
I am currently hosting my cross domain web api on the above url. I have added the custom headers in the web.config file of my web api. I have also added ‘enabled.cors’ property in my stwAPIController. I tried parsing the data in ajax through the following method ‘JSON.parse(data)’,but I am still getting the same error, as mentioned above.
[EnableCors(origins: "http://apitesting.domain.com/api/stw", headers: "*", methods: "*")]
public class STWController : ApiController
{
private cdwEntities db = new cdwEntities();
public IEnumerable getData()
{
var data = db.Database\_CRE\_LoanEvents.Where(c => c.Date.Contains("2015") && c.Loan\_property != null)
.Select(x => new Loan() { Name = x.Deal, loan = x.Loan\_property, evnt = x.Event })
.ToList().Take(3);
return data;
}
}
Any further help, would be very much appreciated.