我正在尝试使用一个简单的网络服务。显然,这将在未来得到进一步加强,但我正在尝试掌握 ajax 调用的基本概念。
网络服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Tools
{
/// <summary>
/// Summary description for CFServices1
/// </summary>
[WebService(Namespace = "http://localhost:51342/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CFServices1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hellow World";
}
}
}
JavaScript
$.ajax({
type: "POST",
url: "CFServices.asmx?/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (response) {
console.log(response);
}
});
ajax 调用似乎工作正常,因为它返回一个错误:
"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
--- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>"
我尝试了几种不同的方法,这些方法导致我发现 URL 属性无效,但由于我收到了实际响应,因此我假设它是正确的。如果我通过浏览器导航到它,它肯定会呈现。即使我尝试通过 asmx 页面调用 Web 服务,它也能正常工作。
我尝试将数据类型更改为纯文本,但这也不起作用。由于所有 Web 方法所做的都是 reutnr 字符串“Hellow World”,因此我不需要传递任何参数,但尝试传递空白值以防万一。一切要么让我回到返回“未定义”的响应、asmx 页面的 html 标记,要么是这个。 “根元素的数据无效。”这告诉我,发送到 Web 服务或从 Web 服务接收的数据不正确或格式不正确。这就是我困惑的地方,因为我无法弄清楚这里可能出了什么问题。
虽然这可能很简单,但我在 SOF 或其他线程上没有找到任何运气。非常感谢任何有用的见解。
最佳答案
当我们从 jquery 调用它时,即从脚本部分,您需要对 Webservice 部分进行以下更改,
[System.Web.Script.Services.ScriptService] // Mark it for accessing from script
public class CFServices1 : System.Web.Services.WebService
我们需要为Web方法设置脚本方法属性,如下
[WebMethod]
[ScriptMethod]
public string HelloWorld()
然后在从ajax调用中调用它时只需删除?在网址部分如下,
$.ajax({
type: "POST",
url: "CFServices.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (response) {
console.log(response);
}});
希望有帮助。
关于javascript - 根级别的数据在 $.ajax 调用中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30228681/