我有以下问题: 我想从后面的代码调用AktualisiereSilomethode,它应该返回一个字符串列表,但我得到的只是一个错误。 似乎ajax方法不会调用代码,但我不知道为什么。 我会非常幸运地得到一些帮助。 如果您需要更多信息或更多代码,请写信给我。
我的代码: 1. Ajax.js
//GET-Abfrage der Silos function GetSiloListe() { alert("In AJAX Aufruf"); $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Karte.aspx/AktualisiereSilo", //data: "{}", (changed it) dataType: "json", success:function (data, textStatus, jQxhr) { alert("Success " + data); }, error: function (textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); }, }); }
function siloUpdate(){ GetSiloListe(); }
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function AktualisiereSilo() As String Dim siloModel As SiloModell = New SiloModell Dim siloList As List(Of SiloModell) = New List(Of SiloModell) Dim Datab As Database = New Database Dim list As String = "" Datab.InitializeDatabase() siloList = Datab.siloListQuery() For Each el In siloList list = list + el.longitude + "/" + el.latitude + ";" Next Return list End Function
答案 0 :(得分:0)
Imports System.Web.Services
<WebMethod()> _
Public Shared Function AktualisiereSilo() As String
Dim siloModel As SiloModell = New SiloModell
Dim siloList As List(Of SiloModell) = New List(Of SiloModell)
Dim Datab As Database = New Database
Dim list As String = ""
Datab.InitializeDatabase()
siloList = Datab.siloListQuery()
For Each el In siloList
list = list + el.longitude + "/" + el.latitude + ";"
Next
Return list
End Function
因为您的数据类型是Json。
(dataType:&#34; json&#34;)
所以试试这个以获得价值。
Public Shared Function AktualisiereSilo() As String
'Json string
Return "[{ ""name"": ""guest01"", ""age"": ""29"", ""height"": ""7"" }]"
End Function
function GetSiloListe() {
alert("In AJAX Aufruf");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/AktualisiereSilo",
dataType: "json",
success:function (data, textStatus, jQxhr) {
var data = $.parseJSON(data.d);
var numOfData = data.length;
for (var i = 0; i < numOfData; i++) {
alert(data[i]["name"]);
alert(data[i]["age"]);
alert(data[i]["height"]);
}
},
error: function (textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); },
});
}
您需要确保字符串返回格式是json类型。
Imports System.Web.Services
<WebMethod()> _
Public Shared Function AktualisiereSilo() As String
Dim siloModel As SiloModell = New SiloModell
Dim siloList As List(Of SiloModell) = New List(Of SiloModell)
Dim Datab As Database = New Database
Dim list As String = ""
Datab.InitializeDatabase()
siloList = Datab.siloListQuery()
For Each el In siloList
list = list + el.longitude + "/" + el.latitude + ";"
Next
Return list
End Function
答案 1 :(得分:0)
如果您没有发送任何内容,则无需添加数据
function GetSiloListe() {
alert("In AJAX Aufruf");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Karte.aspx/AktualisiereSilo",
dataType: "json",
success:function (data, textStatus, jQxhr) { alert("Success " + data); },
error: function (textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); },
});
}
并在你的VB中改变它
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function AktualisiereSilo() As String
Dim siloModel As SiloModell = New SiloModell
Dim siloList As List(Of SiloModell) = New List(Of SiloModell)
Dim Datab As Database = New Database
Dim list As String = ""
Datab.InitializeDatabase()
siloList = Datab.siloListQuery()
For Each el In siloList
list = list + el.longitude + "/" + el.latitude + ";"
Next
Return list
End Function
答案 2 :(得分:0)