手机版

JQuery调用网络服务的方法和4个实例

时间:2021-11-08 来源:互联网 编辑:宝哥软件园 浏览:

您甚至可以为每个ajax请求添加一个后端页面!你是不是在想,尼玛,如果能直接调用C#类文件中的方法,那就太好了?(这里FishLi做了一个框架,有兴趣的可以看看。)但是,你可能忘记了,我们是程序员,我们懒,我们要让电脑为我们做更多的事情!(这里是Pack 13),但实际上,微软和JQuery更早地帮助我们解决了这个小问题。的称呼大致分为以下几种:1。不带参数和返回值的调用前端的JS代码:复制如下: $ ('# btn1 ')。单击(function () {$)。Ajax ({type3360' post ',content type 3360 ' application/JSON;charset=utf-8 ',URL : ' calledbyjquery . asmx/HelloWorld ',data :“{ 0 }”,dataType: 'json ',success : function(JSON){ alert(JSON . d);},错误:函数(错误){ alert(' call error ' error . responsetext);} });});后端WebMethod代码:复制的代码如下: [webmethod]公共字符串hello world(){ return ' hello world ';}用谷歌调试结果:

二、简单参数simple返回值调用前端的JS代码:复制的代码如下: $ ('# btn2 ')。单击(function () {$)。Ajax ({type:' post ',content type 3360 ' application/JSON;Charset=utf-8 ',URL : ' calledbyjquery.asmx/simplereturns',数据3360' {name3360 '张三' } ',datatype3360' JSON ',success3360函数(JSON){ alert(JSON . d);},错误:函数(错误){ alert(' call error ' error . responsetext);} });});后端WebMethod代码:复制的代码如下: [webmethod]公共字符串simple returns(字符串名称){return string.format('您的名字是{0} ',name);}用谷歌调试结果:

三、复杂参数和复杂返回值调用前端的JS代码:复制的代码如下: $ ('# btn3 ')。单击(function () {$)。Ajax ({type:' post ',content type 3360 ' application/JSON;charset=utf-8 ',URL : ' calledbyjquery . asmx/GetStudentList ',data: '{stu:{ID:'6 ',Name:'ff'} ',dataType: 'json ',success 3360 function(JSON){ alert(JSON . d);},错误:函数(错误){ alert(' call error ' error . responsetext);} });});后端WebMethod:复制代码如下:【web method】公共列表学生getstudentlist(studenttu){列表学生学生列表=新列表学生{新学生{id=1,名称='张三' },新学生{ID=2,名称='李四' } };//将客户端传输的实体放回返回值studentList中。添加(stu);返回studentList}用谷歌调试结果:

4.匿名对象调用WebMethod前端的JS代码:复制的代码如下: $ ('# btn4 ')。单击(function () {$)。Ajax ({type:' post ',content type 3360 ' application/JSON;charset=utf-8 ',URL : ' calledbyjquery . asmx/ReturnNoNameClass ',data : ' { 0 } ',dataType: 'json ',success 3360 function(JSON){ alert(JSON . d);},错误:函数(错误){ alert(' call error ' error . responsetext);} });});后端WebMethod代码:复制的代码如下: [webmethod]公共对象returnnonameclass(){ returnnew { id=1,name='张三' };}用谷歌调试结果:

哈哈,在这里,你觉得这么容易吗?妈妈再也不用担心我的学习了。其实很多事情很简单,但是没有人告诉我们,实际开发中我们也没有这种需求,给我们的发展造成了一些障碍。所以,沟通是如此重要!

版权声明:JQuery调用网络服务的方法和4个实例是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。