2014-01-14 24 views
0

我有一個JS腳本:從JS調用ASP.NET頁面和發送響應

$.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,並將其寫入響應。

我的問題是以下。 結果對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&amp;userID=18" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZLyq7A39QtHu0Ngft6E/6ZwTABk29noGDsoP6FKK6UIo" /> </div> <div> </div> </form> </body> </html> 

,但我要的是唯一的JSON響應,而頁面的代碼。我怎樣才能做到這一點?可能嗎?

回答

1

您是否嘗試過在初始請求中將返回數據類型指定爲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(); 
+0

感謝richieahb!它看起來像它的作品!非常感謝 :) – mashet