Web services 使用jQuery使用ASP Web服务
我发现了一些关于这个的其他问题,但它们对我不起作用:(所以我发布了我自己的问题: 我已经在mono中创建了一个ASP.NET Web服务。它的代码是:Web services 使用jQuery使用ASP Web服务,web-services,jquery,mono,Web Services,Jquery,Mono,我发现了一些关于这个的其他问题,但它们对我不起作用:(所以我发布了我自己的问题: 我已经在mono中创建了一个ASP.NET Web服务。它的代码是: using System; using System.Web; using System.Web.Services; namespace testWebservice { public class ws : System.Web.Services.WebService { public ws() {
using System;
using System.Web;
using System.Web.Services;
namespace testWebservice
{
public class ws : System.Web.Services.WebService
{
public ws() {
}
[WebMethod]
public String sendCar()
{
return "44.435006,26.102314";
}
}
}
我正在尝试使用jQuery从网站调用此Web服务:
function getCar()
{
$.ajax( {
type:'POST',
url:'http://127.0.0.1:8080/ws.asmx/sendCar',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success:function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
})
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
}
当我运行该页面时,会收到一条警告,提示“0错误”。
有人能给我一些建议吗?据我所知,您使用的是旧的asp.net 2.0样式的web服务。为什么要将json设置为发送的数据类型?我认为这不适合这些web服务您在服务中的代码不正确。 你至少错过了机会
[System.Web.Script.Services.ScriptService]
类上的属性ws
在几天前,我发布了一个对我有用的样本
请注意,将返回类型从字符串更改为其他类型需要发送真正的json对象。要使服务正常工作,您需要: 在项目中添加对
System.Web.Extension
的引用
使用System.Web.Script.Services;向ws-source添加
加:
到您的ws类
加:
[ws.asmx.cs]
using System;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
namespace testWebservice
{
[ScriptService]
public class ws : System.Web.Services.WebService
{
public ws() {
}
[WebMethod]
public String sendCar()
{
return "44.435006,26.102314";
}
}
}
[web.config]
<?xml version="1.0"?>
<!--
Web.config file for testWebservice.
The settings that can be used in this file are documented at
http://www.mono-project.com/Config_system.web and
http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx
-->
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
<customErrors mode="RemoteOnly">
</customErrors>
<authentication mode="None">
</authentication>
<authorization>
<allow users="*" />
</authorization>
<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"/>
</httpHandlers>
<trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" />
<sessionState mode="InProc" cookieless="false" timeout="20" />
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
<pages>
</pages>
</system.web>
</configuration>
我正在使用mono,我不知道Web服务应该是什么风格。我也不知道json。这都是我在互联网上发现和阅读的内容的改编。System.Web.Script.Services
在我当前的mono运行时mono 4.0.2中缺失(Stable 4.0.2.5/c99aa0c Wed Jun 24 05:31:11 EDT 2015)(64位),是否有方法手动添加它?找出名称空间是哪个DLL的一部分并引用该DLL。
<%@ Page Language="C#" Inherits="testWebservice.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
<title>jQuery Web Service call from Mono using JSON</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function getCar()
{
$.ajax( {
type:'POST',
url:'http://127.0.0.1:8080/ws.asmx/sendCar',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success:function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
})
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button runat="server" id="btnTest" Text="Call WebService" OnClientClick="getCar(); return false;" />
</form>
</body>
</html>
using System;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
namespace testWebservice
{
[ScriptService]
public class ws : System.Web.Services.WebService
{
public ws() {
}
[WebMethod]
public String sendCar()
{
return "44.435006,26.102314";
}
}
}
<?xml version="1.0"?>
<!--
Web.config file for testWebservice.
The settings that can be used in this file are documented at
http://www.mono-project.com/Config_system.web and
http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx
-->
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</assemblies>
</compilation>
<customErrors mode="RemoteOnly">
</customErrors>
<authentication mode="None">
</authentication>
<authorization>
<allow users="*" />
</authorization>
<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"/>
</httpHandlers>
<trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" />
<sessionState mode="InProc" cookieless="false" timeout="20" />
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
<pages>
</pages>
</system.web>
</configuration>