簡體   English   中英

從aspx中調用aspx.cs中的ajax方法

[英]Calling a method with ajax in aspx.cs from aspx

我試圖從我的aspx頁面調用一個方法。 這個方法可以在aspx.cs頁面上找到,但它會引發錯誤。 你明白出了什么問題嗎?

ajax腳本

<script type="text/javascript">
     function OnSucceeded(response) {
         alert(response);
     }
     function OnFailed(error) {
         alert(error);
     }         //Default.aspx
     function insertMarker() {
         var usernameName = 'username';
         var usernameVal = document.getElementById('<%=hdnUsername.ClientID%>').value;

         var latitudeName = 'latitudeStr';
         var latitudeVal = document.getElementById('<%=hdnMarkerLatitude.ClientID%>').value;

         var longituteName = 'longitudeStr';
         var longitudeVal = document.getElementById('<%=hdnMarkerLongitude.ClientID%>').value;

         var iconName = 'markerIcon';
         var iconVal;
         if (document.getElementById('blueMarker').checked) {
             iconVal = 'images/blueMarker.png';
         }
         if (document.getElementById('greenMarker').checked) {
             iconVal = 'images/greenMarker.png'
         }
         if (document.getElementById('pinkMarker').checked) {
             iconVal = 'images/pinkMarker.png';
         }

         var titleName = 'name';
         var titleVal = document.getElementById('<%=title.ClientID%>').value;

         var descriptionName = 'description';
         var descriptionVal = document.getElementById('<%=description.ClientID%>').value;

         $.ajax({
             type: "POST",
             url: "mapping.aspx/insertNewMarker",
             data: {"username" : usernameVal, "longitudeStr":longitudeVal, "latitudeStr" :latitudeVal, "markerIcon":iconVal, "name" : titleVal, "description" :descriptionVal},
             contentType: 'application/json; charset=utf-8',
             dataType: 'json',
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
             },
             success: function (result) {
                 alert("We returned: " + result.d);
             }
         });
     }

     </script>

網站設計保存標記標題說明
保存

Aspx.cs方法。

[ScriptService]
public partial class mapping: System.Web.UI.Page
{
    [WebMethod]
    private static void insertNewMarker(string username, string longitudeStr, string latitudeStr,  string markerIcon, string name, string description)
    {

       //My Code
    }


}

錯誤

您的服務器端web方法不能是私有的 ,您必須將其更改為公共

webmethods上的MSDN文檔

當您在托管代碼的Web服務,您指示可通過Web服務通過一個公共方法的方法聲明之前放置WebMethod屬性的方法。 私有方法不能作為Web服務的入口點,盡管它們可以位於同一個類中,並且Web服務代碼可以調用它們。

像這樣更改您的data

data:JSON.stringify({username : usernameVal, longitudeStr:longitudeVal, latitudeStr :latitudeVal, markerIcon:iconVal, name : titleVal, description :descriptionVal}),

您需要以具有特定格式的json stirng傳遞數據。 如果你使用JSON.stringify數據將被轉換為json字符串,如果你不使用它,你必須將every paremter and its value in quotes傳遞給這樣的every paremter and its value in quotes

data:"{username:'" + usernameVal + "',............}",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM