如何使用jQuery执行简单的Web Api Get [英] How do I use jQuery to execute a simple Web Api Get
本文介绍了如何使用jQuery执行简单的Web Api Get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除Ajax函数之外的简单Web API示例将不会返回成功或错误警报:
http:// localhost:45240 / api / Values / 1
Simple Web API example except the Ajax function will not return a success or an error alert:
http://localhost:45240/api/Values/1
public class ValuesController : ApiController
{
public IEnumerable<Customer> Get()
{
return new List<Customer>(){
new Customer() { CustomerId = 1, CustomerName = "This" },
new Customer() { CustomerId = 2, CustomerName = "That" }
};
}
public Customer Get(int id)
{
if (id == 1)
{
return new Customer() { CustomerId = 1, CustomerName = "This" };
}
return null;
}
}
WebApiTest.aspx
WebApiTest.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function () {
$.ajax({
url: 'http://localhost:45240/api/Values/1',
type: 'GET',
contentType: 'application/json; charset=utf-8',
data: '{}',
datatype: 'json',
success: function (data){
alert("works");
},
error: function (x, e) {
alert("Error: " + x.responseText);
}
});
});
});
</script>
<asp:Button ID="Button1" runat="server" Text="Button" />
推荐答案
(document).ready(function() {
(document).ready(function () {
('#Button1')。click(function(){
('#Button1').click(function () {
.ajax({
url:'http:// localhost:45240 / api / Values / 1',
类型:'GET',
contentType:'application / json; charset = utf-8',
data:'{}',
datatype:'json',
success:function(data){
alert(works);
},
错误:function(x,e){
alert(错误:+ x.responseText);
}
});
});
});
< / script >
< asp:按钮 ID = Button1 runat = server 文字 = 按钮 / >
.ajax({ url: 'http://localhost:45240/api/Values/1', type: 'GET', contentType: 'application/json; charset=utf-8', data: '{}', datatype: 'json', success: function (data){ alert("works"); }, error: function (x, e) { alert("Error: " + x.responseText); } }); }); }); </script> <asp:Button ID="Button1" runat="server" Text="Button" />
这篇关于如何使用jQuery执行简单的Web Api Get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文