2017-09-15 55 views
0

我在.aspx頁面的codebhind後面有一個web方法,但是當我嘗試訪問它時,並不會觸發webmethod只有頁面。返回狀態是200,頁面正在被調用,但該方法被忽略。不管我是否使用name.aspx/GetData或name.aspx/Anything,結果都是200,但是這個方法沒有被觸發。我用jQuery Ajax和Postman進行了測試。獲取和發佈嘗試。 有沒有什麼可以改變的web.config或任何其他的東西。.NET Web方法不僅僅是從javascript調用頁面

$.ajax({ 
         url: '/adm/clientAccess.aspx/MyMethodInexistent', 
         data: {}, 
         type: 'POST', 
         contentType: 'application/x-www-form-urlencoded', 
         dataType: 'html', 
         success: function (data) { 
          //I GET HERE even if the method doesn't exist, and if it exists, it doesn't return data. 
          alert(1); 
         }, 
         error: function (response) { 
          alert(response.responseText); 
         } 
        } 
        ); 

enter image description here

enter image description here

enter image description here

+0

如果您沒有發佈任何代碼,您會比我們更瞭解這個問題。我們怎麼可能幫你?請閱讀如何提出問題的指導原則:https://stackoverflow.com/help/mcve – JuanR

+0

對不起,我現在添加了。 – AlvaroCryptogram

+0

您正在使用哪個版本的.NET?它應該是'url:'/ adm/clientAccess.aspx/MyMethod','調用該方法。嘗試在方法中添加一個斷點,以查看是否能夠達到它。 – Niladri

回答

1

你可以嘗試用下面的代碼,確保路徑到的方法是請在下面的url中更正。如果您使用的是腳本管理器級別,也啓用頁面方法。

$.ajax({ 
    url: '/adm/clientAccess.aspx/MyMethod', 
    data: {}, 
    type: 'POST', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    async: "true", 
    success: function (data) { 
     console.log(data); 
     //alert(1); 
    }, 
    error: function (response) { 
     alert(response.responseText); 
    } 
} 
); 
+0

嗨。我試圖做到這一點,使用EnablePageMethods添加scriptManager,但結果是一樣的,它返回整個頁面,它不會觸發該方法,我仍然可以調用一個現有的方法。 – AlvaroCryptogram

+0

哇,好消息,它與contentType一起工作:「application/json; charset = utf-8 」。這是一切的組合。非常感謝! – AlvaroCryptogram

1

需要使用ScriptManager的元素,使頁面的方法:

<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true"/> 
+0

我也試過這個,但沒有工作。謝謝。 – AlvaroCryptogram

相關問題