我正在尝试构建一个jquery插件,它使用AJAX POST请求命中VB.NET Web服务。现在我想站出发送JSON的请求然后在console.log中接收它。我有它工作然后我改变了命名约定我现在不断收到404错误。我已经发布了整个web.config文件,以防万一。只需将数据发送到Web服务,然后将相同的数据返回到控制台。到目前为止,这是我的代码:
JS:
(function($){
$.fn.loadMGMT = function(options) {
$this = $(this);
data = $.extend({
level: 'ORA',
titles: 'Administrator'
}, options );
$.ajax({
type: 'POST',
cache: false,
headers: { 'Access-Control-Allow-Origin': '*' },
url: '/webapps/webservices/Intranet/getActiveDirectoryInfo.aspx/leadership',
data: "{leader:" + JSON.stringify(data) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
}).done(function(api){
console.log(api);
});
};
}(jQuery));
$(document).ready(function(e) {
$('#leaders').loadMGMT({
level: "DORA",
titles: 'Regional Administrator'
});
});
网络服务:
Imports System.Collections.Generic
Partial Class VB
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()> _
Public Shared Function leadership(ByVal leader As Leader) As Leader
Return leader
End Function
End Class
Public Class Leader
Private _level As String
Public Property Name As String
Get
Return _level
End Get
Set(ByVal value As String)
_level = value
End Set
End Property
Private _titles As String
Public Property Name As String
Get
Return _titles
End Get
Set(ByVal value As String)
_titles = value
End Set
End Property
End Class
Web.Config中:
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\vx.x\Config
-->
<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>
<identity impersonate="true" userName="NRCDOMAIN\r2cfsvc" password="LkE^ehf~UXk~JAPf"/>
<!--
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.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<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="Windows"/>
<!--
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>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<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 appliesTo="v2.0.50727" 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>
答案 0 :(得分:0)
尝试以下操作,这些天找人使用VB并不容易!
我的代码也处理CORS问题,html可以托管在www.a.com(或作为Android或iphone中的本机应用程序),Web服务可以托管在www.b.com
使用适用于Apache Cordova CTP3.1的工具在VS 2015社区或VS 2013社区更新4中创建空白解决方案
在解决方案中创建一个空白的JavaScript Cordova项目。
使用以下代码替换index.html:
<html> <head> <script type="text/javascript"> function funCheckWS() { var tblLogin = {}; tblLogin.ReturnCode = "0000"; jQuery.support.cors = true; $.ajax({ type: 'POST', url: 'http://localhost:8080/wsMain.asmx/funCheckWS', data: "{tblLogin:" + JSON.stringify(tblLogin) + "}", contentType: 'application/json; charset=utf-8', dataType: 'json', crossDomain: true, async: false, success: function (r) { tblLogin.ReturnCode = r.d.ReturnCode; alert("Return Code from Web Services:" + tblLogin.ReturnCode); }, error: function (xhr, ajaxOptions, thrownError) { // Browser does not support WS //alert(xhr.status); //alert(thrownError); } }); } </script> </head> <body> </body> </html>
创建一个Visual Basic&#34; Asp.Net Web应用程序&#34;,以下内容涉及此项目:
在Web.config中,在
<webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices>
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="POST,OPTIONS,GET"/> <add name="Access-Control-Allow-Headers" value="Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,Accept"/> <add name="Access-Control-Allow-Origin" value="*"/> </customHeaders> </httpProtocol>
Public Class tabLogin Private _ReturnCode As String Public Property ReturnCode As String Get Return _ReturnCode End Get Set(ByVal value As String) _ReturnCode = value End Set End Property End Class
Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel Imports System.Web.Script.Serialization Imports System.Web.Script.Services Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web <System.Web.Script.Services.ScriptService()> _ <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class wsMain Inherits System.Web.Services.WebService Private tblLogin As tabLogin <WebMethod()> _ <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ Public Function funCheckWS(tblLogin As tabLogin) As tabLogin tblLogin.ReturnCode = "0001" Return tblLogin End Function End Class
构建此项目并使用IIS使用端口8080托管它。
构建javascript cordova项目并将其部署到手机或使用您喜欢的任何端口在IIS中托管index.html,例如8070。
在手机中,运行应用程序。或者在任何桌面浏览器中,使用http://localhost:8070/index.html
然后会出现一个消息框,其中包含ReturnCode =&#34; 0001&#34;,它是从Web服务返回的。