gpt4 book ai didi

web-services - 使用 jQuery 使用 ASP Web 服务

转载 作者:行者123 更新时间:2023-12-01 01:27:16 25 4
gpt4 key购买 nike

我发现了一些与此相关的其他问题,但它们对我不起作用:(所以我发布了我自己的问题:

我用 Mono 制作了一个 ASP.NET Web 服务。其代码为:

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 错误”。有人可以给我一些建议吗?

最佳答案

要使服务正常工作,您需要:

向您的项目添加对 System.Web.Extension 的引用.

添加using System.Web.Script.Services;到您的 ws 源。

添加:

[ScriptService]

到你的ws类(class)。

添加:

    <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> 下的 web.config 中标签。

如果您调用getCar()通过按钮单击添加 return false;在方法调用之后,这样点击就不会导致回发。

例如:

[默认.aspx]

<%@ 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>

[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>

关于web-services - 使用 jQuery 使用 ASP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com