用Jquery访问WebService并返回Json的代码第2/3页

 更新时间:2008年09月15日 22:37:34   作者:  
经常会用JavaScript访问asp.net的Webservice的需求,通常的方法是用asp.net ajax来解决,但asp.net ajax框架在不国内并不经常被使用。

test.html

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script src="jquery-1.2.2.js" type="text/javascript"></script>
</head>
<body>

<input type="button" onclick="requestHelloWorld();" value="请求HelloWorld" />
<input type="button" onclick="requestCreateUser();" value="请求CreateUser" />
<script type="text/javascript">


function requestHelloWorld(){
WebService('ws1.asmx/HelloWorld',function(obj){alert(obj);});
}

function requestCreateUser(){
WebService('ws1.asmx/CreateUser',function(obj){alert(obj.Name);},'{name:"林子",age:25}');
}


function WebService(url,callback,pars){
$.ajax({
data:pars,
url: url,
type: "POST",
contentType: "application/json;utf-8",
dataType:'json',
cache: false,
success: function(json){
callback(json.d);
},
error:function(xml,status){
if(status=='error'){
try{
var json = eval('(' + xml.responseText + ')');
alert(json.Message+'\n'+json.StackTrace);
}catch(e){}
}else{
alert(status);
}
},
beforeSend:function(xml){
if(!pars) xml.setRequestHeader("Content-Type", "application/json;utf-8")
}
});
}
</script>
</body>
</html>

web.config为vs2008在.net 3.5状态下创建的默认的 也有很重要的作用 这里贴出来
复制代码 代码如下:

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="None"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

相关文章

  • asp将本地的文件上传到服务器

    asp将本地的文件上传到服务器

    如果你想把自己机器的图片或者文件放到服务器上该怎么办呢?<BR>可选有三种办法ftp上传、用u盘拷贝到服务器上、如果服务器支持asp上传功能,用网页浏览器将文件上传到服务器上
    2015-09-09
  • 详解c# .net core 下的网络请求

    详解c# .net core 下的网络请求

    本篇文章主要介绍了详解c# .net core 下的网络请求,大致介绍下在.net core 下如何进行http请求,主要仍然是GET和POST方法,有兴趣的可以了解下
    2017-05-05
  • 使用ASP.NET Web API构建Restful API

    使用ASP.NET Web API构建Restful API

    这篇文章介绍了使用ASP.NET Web API构建Restful API的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • asp.net实现文件无刷新上传方法汇总

    asp.net实现文件无刷新上传方法汇总

    本文给大家介绍的是asp.net实现文件无刷新上传的2种方法,分别是使用swfupload插件和uploadify插件,讲述的十分细致全面,附上示例,有需要的小伙伴可以参考下。
    2015-06-06
  • C#读取中文字符及清空缓冲区的实现代码

    C#读取中文字符及清空缓冲区的实现代码

    有一个txt的中英文语料库文件,内容是英文一句中文一句相间的,共3000行,需要把英文句和中文句分开,放在单独的txt文件中。
    2010-12-12
  • ASP.NET文件上传控件Uploadify的使用方法

    ASP.NET文件上传控件Uploadify的使用方法

    这篇文章主要为大家详细介绍了ASP.NET文件上传控件Uploadify的使用方法,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • asp.net 常用字符串处理方法

    asp.net 常用字符串处理方法

    asp.net 常用字符串处理方法,学习asp.net的朋友可以参考下。
    2010-12-12
  • ASP.NET 控件开发系列之图片切换web控件

    ASP.NET 控件开发系列之图片切换web控件

    刚开始学习控件开发,写了一个web图片切换控件,欢迎大家拍砖.
    2010-04-04
  • Visual Studio 2017安装心得总结

    Visual Studio 2017安装心得总结

    这篇文章主要为大家详细总结了Visual Studio 2017安装心得,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • ADO.NET 读取EXCEL的实现代码((c#))

    ADO.NET 读取EXCEL的实现代码((c#))

    经常需要在数据库与Execl之间互导数据。net时代,ADO.NET可以使用使用Microsoft.Jet.OleDb访问访问Excel,网上已经有很多类似的资源,最典型也是最简单的可能如下:(asp.net环境)
    2012-12-12

最新评论