如何在jquery中使用MVC4 Webapi [英] how to Consume MVC4 Webapi in jquery

查看:48
本文介绍了如何在jquery中使用MVC4 Webapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在mvc4中创建了web api现在我通过html页面中的j查询来消费它但是它每次调用错误函数而不是成功函数以下是函数



Hi,
I have created web api in mvc4 now I am consuming it through j query in html page but it calls error function each time instead of success function following is the function

function GetAllEmployee() {
           //$("#emps").empty();

           $.ajax({
               url: 'http://localhost:54374/api/Employee/';,
               type: 'GET',
               dataType: 'json',
               success: function (data) {
                   alert('yes');

                   for (var i = 0; i < data.length; i++) {

                       $("<table><tbody><tr></tr></tbody></table><table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Id + "<table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Name + "<table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Salary + "<table><tbody><tr><td>" + data[i].City + "</td></tr></tbody></table>").appendTo("#students");
                   }
               },
               error: function (xhr) {
                   alert('Some error occured');

                   alert(xhr);
               }
           });


       }



请帮我解决如何使用函数正确调用函数


please help me how to use the fucntion to call function properly

推荐答案

(#emps)。empty();
("#emps").empty();


.ajax({
url:' http:// localhost:54374 / api / Employee /' ;,
类型:' GET'
dataType:' json'
成功:函数(数据){
alert(' < span class =code-string> yes');

for (var i = 0 ; i< data.length; i ++){
.ajax({ url: 'http://localhost:54374/api/Employee/';, type: 'GET', dataType: 'json', success: function (data) { alert('yes'); for (var i = 0; i < data.length; i++) {


<表>< TBODY>< TR>< / TR>< / TBOD Y'GT;< /表><表>< TBODY>< TR><表>< TBODY>< TR><表>< TBODY>< TR>< TD>< / TD>< / TR>< / tbody的>< /表>< / TR>< / tbody的>< /表>< / TR>< / tbody的>< /表> + data [i] .Id + < table>< tbody>< tr>< table> ;< TBODY>< TR><表>< TBODY>< TR>< TD>< / TD>< / TR>< / tbody的>< /表>< / TR> < / tbody>< / table>< / tr>< / tbody>< / table> + data [i] .Name + < table>< tbody>< tr>< table>< tbody>< tr>< table>< tbody>< tr>< ; TD>< / TD>< / TR>< / tbody的>< /表>< / TR>< / tbody的>< /表>< / TR>< / tbody的>< / table> + data [i] .Salary + < table>< tbody>< tr>< td> + data [i] .City + < / td>< / tr>< / tbody>< / table>)。appendTo( #students);
}
},
错误:函数(xhr){
alert(' 发生了一些错误');

alert(xhr);
}
});


}
("<table><tbody><tr></tr></tbody></table><table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Id + "<table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Name + "<table><tbody><tr><table><tbody><tr><table><tbody><tr><td></td></tr></tbody></table></tr></tbody></table></tr></tbody></table>" + data[i].Salary + "<table><tbody><tr><td>" + data[i].City + "</td></tr></tbody></table>").appendTo("#students"); } }, error: function (xhr) { alert('Some error occured'); alert(xhr); } }); }



请帮助我如何使用功能正常调用功能


please help me how to use the fucntion to call function properly


这篇关于如何在jquery中使用MVC4 Webapi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆