我已经创建了 ASP.NET Web API 服务并托管了它。当我检查 Fiddler 时,该服务工作正常,但是当我通过 jQuery ajax 调用该服务时,它不起作用,我也没有收到任何错误。我尝试调用不同的 API 方法但遇到相同的问题
网络接口:
namespace WebApi.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public string Post(Interview Interview)
{
return "Date: " + Interview.DateOfSurvey + " \n rdInterviewObtained: " + Interview.rdInterviewObtained + "\n txtWhowasinterOther: " + Interview.txtWhowasinterOther;
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
阿贾克斯调用:
$("#btnSave2").click(function () {
var person = { DateOfSurvey: '01-01-2012', rdInterviewObtained: 'N', txtWhowasinterOther: 'N' };
alert('inside button click');
jQuery.support.cors = true;
$.ajax({
url: "http://url/api/Values",
type: 'GET',
//data: person,
//dataType: 'json',
success: function (data) {
alert('success');
},
error: function (x, y, z) {
alert('error');
alert(x + '\n' + y + '\n' + z);
}
});
});