[英]Calling ASP.NET page from a JS and sending Response
我有一個JS腳本:
$.ajax({
type: "GET",
url: ServiceAddress + "/Service.aspx?action=LoadRandomImagePath&userID=" + USER_ID,
success: function (result) {
if (result.LoadRandomImagePathResult.ID != 0) {
//something
}
調用Service.aspx時:
Response.Write(svc.LoadRandomImagePath(int.Parse(Request.QueryString["userID"])));
svc.LoadRandomImagePath返回JSON並將其寫入Response。
我的問題如下。 JS端的結果是Service.aspx的完整代碼:
{JSON HERE}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="Service.aspx?action=LoadRandomImagePath&userID=18" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZLyq7A39QtHu0Ngft6E/6ZwTABk29noGDsoP6FKK6UIo" /> </div> <div> </div> </form> </body> </html>
但是我想要的只是JSON響應,而沒有頁面代碼。 我怎樣才能做到這一點? 可能嗎?
您是否曾嘗試在初始請求中將返回數據類型指定為JSON?
$.ajax({
type: "GET",
dataType: "json",
url: ServiceAddress + "/Service.aspx?action=LoadRandomImagePath&userID=" + USER_ID,
success: function (result) {
if (result.LoadRandomImagePathResult.ID != 0) {
//something
}
此外,您將希望清除所有已寫入的內容,並更改JSON頁面的數據類型:
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(svc.LoadRandomImagePath(int.Parse(Request.QueryString["userID"])));
Response.End();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.