jquery用ajax调用webservice
2017-07-03 14:09
288 查看
前端代码:
后端代码:
为了能在JQ里跨域,需要以上代码放在<system.webServer>和</system.webServer>之间
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery/jquery-2.0.3.min.js"></script> </head> <body> <script type="application/javascript"> $(function () { var url = "http://www.air.com/test.asmx/UserLogin"; $.ajax({ type: "GET", url: url, data: {userName: 'air', userPwd: '123'}, dataType: "html", success: function (s) { alert(s) var jsonstr=s.replace(/<\/?.+?>/g,""); var json =$.parseJSON(jsonstr); alert(json.user.id); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(errorThrown); }, }); }) </script> </body> </html>
后端代码:
/// <summary> /// test 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class test : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet=true)] public string UserLogin(string userName, string userPwd) { string returnVal = ""; returnVal = "{\"user\":{\"id\":\"201707031059\",\"name\":\"" + userName + "\",\"pwd\":\"" + userPwd + "\"}}"; return returnVal; } }Web.config:
<webServices> <protocols> <add name="HttpPost" /> <add name="HttpGet" /> </protocols> </webServices> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpHandlers>为了能在JQ里调用webservice,需要以上代码放在<system.web>和</system.web>之间
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> <add name="Access-Control-Allow-Headers" value="x-requested-with"/> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol>
为了能在JQ里跨域,需要以上代码放在<system.webServer>和</system.webServer>之间
相关文章推荐
- Jquery利用ajax调用asp.net webservice的各种数据类型(总结篇)
- Jquery + Ajax 调用 webService
- jquery-ajax之1:调用 Asp.Net WebService 的详细例子
- Jquery之Ajax 调用 xmlWebService篇
- 使用jquery的ajax方法调用webservice服务
- jQuery入门(9):Ajax(调用页面方法和WebService)
- Jquery + Ajax 调用 webService
- Jquery + Ajax 调用 webService(转)
- ajax、jquery调用webservice
- [JQuery]- 利用jquery的ajax调用后台的WebService公共方法和网页的静态方法
- jquery中通过ajax调用webservice传递数组参数的问题
- jQuery入门(10):Ajax(利用WebService调用用户控件生成HTML)
- Jquery + Ajax 调用 webService
- jQuery使用Ajax方法调用WebService
- jQuery带参数的ajax调用WebService
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
- Jquery + Ajax调用webService实例代码(asp.net)
- Jquery + Ajax调用webService实例代码(asp.net)
- jquery中通过ajax调用webservice传递数组参数的问题
- jQuery中通过ajax调用webservice传递数组参数的问题实例详解