当前位置: 移动技术网 > IT编程>开发语言>.net > 用Jquery访问WebService并返回Json的代码第1/3页

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

2018年04月20日  | 移动技术网IT编程  | 我要评论

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>

2

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网