2015-05-29 42 views
0

我阿賈克斯後不運行我的身後方法的代碼,因此不會返回任何數據被調用。代碼隱藏功能不被阿賈克斯POST

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="AspTest.Test" %> 

Test.aspx文件(阿賈克斯腳本)

<script> 
       $(".avatarThumb").click(function() { 
        $.ajax({ 
         type: "POST", 
         url: "Test.aspx/MyMethod", 
         //data: {"s": "Some data passed through" }, 
         //contentType: 'application/json; charset=utf-8', 
         //dataType: 'json', 
         success: function (response) { 
          alert(response.d); //Returns "undefined" 
         }, 
         failure: function(response) { 
          alert(response); 
         } 
        }); 
       }); 
      </script> 

刪除的contentType和數據類型,則達到成功,但不運行代碼隱藏方法。在contentType和/或dataType處於活動狀態時,它不會達到成功或失敗。 FF螢火蟲不會顯示任何錯誤。

* Test.aspx.cs代碼隱藏方法

 [System.Web.Services.WebMethod] 
     public static string MyMethod() 
     { 
(*)   Debug.WriteLine("MyMethod called!"); // (*) Breakpoint never reached 

      return "Method called!"; 
     } 
+0

寫[HttpPost]。 – developer

+0

@krillgar沒有結果,是'RUNAT = 「服務器」'甚至不允許呢?它不會在智能感知中彈出。 – Madmenyo

+0

@developer我無權訪問該類。我需要'Microsoft.Activities.dll'。從未導入.dll,但我會嘗試。 – Madmenyo

回答

1

像這樣改變你的代碼。它應該工作正常

var SendData = {}; 
$.ajax({ 
     type: "POST", 
     url: "Test.aspx/MyMethod", 
     data: JSON.stringify(SendData), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) { 
      alert(data.d); 
     }, 
     error: function (result) { 
      console.warn(result.statusText); 
     } 
    }); 

你的C#方法:

[System.Web.Services.WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public static string MyMethod() 
{ 

    return "Method called!"; 
} 

上面的代碼工作,如果烏拉圭回合頁面Test.aspx的是在項目的根目錄。如果不改變像基於文件的位置,深度「../Test.aspx/MyMethod」 URL參數值。

+0

我得到內部'服務器錯誤'不得不輸出它作爲警報,因爲'console.warn()似乎沒有輸出到任何地方。 – Madmenyo

+0

服務器錯誤來自'data'屬性。 'MyMethod'正在等待一個名爲's'的參數。我終於觸發了斷點,所以'JSON.stringify()'是解決方案。現在我嘗試獲取一些在服務器和客戶端之間傳遞的數據。 – Madmenyo

+0

是的,我從代碼後面刪除了參數。現在它工作正常。在控制檯中記錄java腳本錯誤而不是alert會很好。因爲錯誤函數中的結果參數是一個對象,並且您無法在警告框中展開和查看。 –

0

改變數據如下併發送AJAX。

$.ajax({ 
        type: "POST", 
        url: "Test.aspx/MyMethod", 
        data: JSON.stringify({"s": "Some data passed through" }), 
        contentType: 'application/json; charset=utf-8', 
        //dataType: 'json', 
        success: function (response) { 
         alert(response.d); //Returns "undefined" 
        }, 
        failure: function(response) { 
         alert(response); 
        } 
       }); 

現在webmethod應該匹配通過ajax發送的參數。

[System.Web.Services.WebMethod] 
    public static string MyMethod(string s) 
    { 

     return "Method called!"; 
    } 
0

試試這個:

$(".avatarThumb").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: '<%=ResolveUrl("~/Test.aspx/MyMethod")%>', 
     contentType: 'application/json; charset=utf-8', 
     success: function (response) { 
      alert(response.d); 
     }, 
     failure: function(response) { 
      alert(response); 
     } 
    }); 
}); 

確認類型,GET或POST。你命名的MyMethod方法之前

+0

這不會成功或失敗。我也不會在螢火蟲中發現錯誤。 – Madmenyo

+0

根據您的項目檢查url –

+0

頁面位於項目的根目錄。它有一個名字空間'AspTest'。無論如何,我用Rakesh的答案來解決它。 – Madmenyo