gpt4 book ai didi

jQuery AJAX Web 服务仅在本地运行

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

我有一个简单的 ASP.NET Web 服务

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
}

[WebMethod]
public string SetName(string name) {
return "hello my dear friend " + name;

}
}

对于此 Web 服务,我创建了虚拟目录,因此我可以通过点击来接收访问权限 http://localhost:89/Service.asmx .

我尝试使用 jQuery 通过简单的 html 页面来调用它。为此,我使用函数

CallWS() {
$.ajax({
type: "POST",
data: "{'name':'Pumba'}",
dataType: "json",
url: "http://localhost:89/Service.asmx/SetName",
contentType: "application/json; charset=utf-8",
success: function (msg) {
$('#DIVid').html(msg.d);

},
error: function (e) {
$('#DIVid').html("Error");
}
});

最有趣的事实:如果我使用 WebService 在项目中创建 html 页面并将 url 更改为 Service.asmx/SetName ,一切都会正常工作。但是,如果我尝试远程调用此 Web 服务 - 成功函数可以工作,但 msg 为空。

之后我尝试通过 SOAP 调用该服务。它是一样的 - 在本地它工作得很好,但在远程 - 一点也不。

  var ServiceUrl = 'http://localhost:89/Service.asmx?op=SetName'; 

function beginSetName(Name) {
var soapMessage = '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <SetName xmlns="http://tempuri.org/"> <name>' + Name + '</name> </SetName> </soap:Body> </soap:Envelope>';
$.ajax({
url: ServiceUrl,
type: "POST",
dataType: "xml",
data: soapMessage,
complete: endSetName,
contentType: "text/xml; charset=\"utf-8\""
});

return false;
}

function endSetName(xmlHttpRequest, status)
{
$(xmlHttpRequest.responseXML)
.find('SetNameResult')
.each(function () {
var name = $(this).text();
alert(name);
});
}

在这种情况下,状态的值为“parseerror”。你能帮我解决这个问题吗?我应该怎么做才能通过 jQuery 通过 url 远程调用另一个 WebService?

预先感谢您,格雷格

最佳答案

由于Same Origin Policy ,您将需要修改 AJAX 以使用 JSONP 而不是 JSON。查看jQuery Cross-Domain Ajax Guide .

关于jQuery AJAX Web 服务仅在本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3045882/

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