我尝试通过json格式获取我的Web服务数据,如下所示:
Imports System.Web
Imports System.Web.Script.Services
Imports System.Web.Services
Imports System.Web.Services.Protocols
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()>
<WebService(Namespace:="http://tempuri.org/")>
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()>
Public Function GetAllRss() As List(Of Rss)
Dim db As EMCEntities1 = New EMCEntities1()
Dim RssList As List(Of Rss) = db.Rss.ToList()
Return RssList
End Function
End Class
ajax:
$.ajax({
type: "POST",
url: "/WebService.asmx/GetAllRss",
success: function (result) {
console.log(result);
}
});
在这种情况下,我是否以XML格式获取数据?
该怎么做?
答案 0 :(得分:0)
Web服务要么需要返回编码为JSON的数据,要么可以使用jQuery.parseXML()来解析XML响应。
可能存在一种使用“ json2xml”之类的库将json转换为XML的方法。
这可以帮助: https://goessner.net/download/prj/jsonxml/
有关XML和JSON转换的其他信息: https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
答案 1 :(得分:0)
我用这个ajax解决了
$.ajax({
type: "POST",
url: "/WebService.asmx/GetAllRss",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result);
}
});