我有一個簡單的Web服務,我試圖利用它。顯然,這將會更加強化,但是我正在努力掌握ajax調用的基本概念。
Web服務
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>"
我已經試過一對夫婦各色nt事件導致我的URL屬性的路徑無效,但由於我得到了實際的響應,我假設它是正確的。如果我通過瀏覽器導航到它,它肯定會呈現。即使我嘗試通過asmx頁面調用Web服務,它也可以工作。
我已經嘗試將數據類型更改爲純文本,但這也不起作用。由於所有的web方法都是reutnr字符串'Hellow World',所以我不需要傳遞任何參數,只是爲了防止傳入空白值。一切都會讓我回到返回「未定義」的響應,即asmx頁面的html標記或此。 '根元素處的數據無效'。這告訴我要麼發送到Web服務或從Web服務收到的數據不正確或沒有正確的格式。這是我被掛斷的地方,因爲我無法弄清楚這裏可能會出現什麼問題。
雖然這可能很簡單,但我在SOF或其他線程上找不到任何運氣。任何有用的見解是非常感謝。
我認爲將WebMethod必須是靜態的,而且我認爲在JavaScript中的反應是data.d - WebForms的廣告煩人的「d」 - 但最重要的,爲什麼你使用這個過時的框架,使用MVC –