0
我是jQuery的新手,我正在實現一個我在CodeProjects上找到的示例。從jQuery回調返回數據ASP.NET 2.0
我需要的是從我打電話給PageMethod返回的圖像名和新圖像索引獲取一個字符串。 但是,每次我嘗試通過Response.Write返回除數之外的其他值時,回調將失敗,並進入錯誤函數。
$(document).ready(function() {
var imageIndex = $("[id$='hdn_imageIndex']");
var app_path = $("[id$='hdn_app_path']");
$("#btn_next").click(function() {
var json = "{'Index':'" + imageIndex.val() + "'}";
var ajaxPage = app_path.val() + "/JSONProcessor.aspx?NextImage=1"; //this page is where data is to be retrieved and processed
var options = {
type: "POST",
url: ajaxPage,
data: json,
contentType: "application/json;charset=UTF-8",
dataType: "json",
async: false,
success: function(result) {
alert("success: " + result.d);
// I want my return value from my PageMethod HERE.
},
error: function(msg) { alert("failed: " + msg.d); }
};
var returnText = $.ajax(options).responseText;
});
});
的PageMethod的在JSONProcessor.aspx看起來是這樣的:從的WebMethods
public void NextImage()
{
System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string line = "";
line = sr.ReadToEnd();
JObject jo = JObject.Parse(line);
int newImageIndex = -1;
int oldImageIndex = int.Parse(Server.UrlDecode((string)jo["Index"]));
List<string> images = (List<string>)Session["ShowHouseImages"];
int noOfImages = images.Count;
if (noOfImages > 0)
{
if (oldImageIndex == noOfImages - 1)
{
newImageIndex = 0;
}
else
{
newImageIndex = oldImageIndex + 1;
}
string[] result = ChangeImage(newImageIndex, images);
Response.StatusCode = 200;
Response.Write("1");
// What I REALLY WANT TO RETURN IS THIS
// Response.Write(string.Format("{0};{1};{2}", result[0], result[1], result[2]));
}
Response.Write("0");
}
JSON回報似乎並沒有成爲.NET 2.0的一部分。這就是爲什麼我這樣做。希望有人能幫助我。
你保存了一天..你真的..謝謝! – 2009-12-17 09:20:58
歡迎你( - 。 – Audrius 2009-12-17 10:05:09