从aspx中调用aspx.cs中的ajax方法

时间:2015-05-28 06:05:06

标签: c# jquery asp.net ajax

我正在尝试从我的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
    }


}

Error

2 个答案:

答案 0 :(得分:4)

您的服务器端网络方法不能私有,您必须将其更改为 public

来自MSDN documentation on webmethods

  

在托管代码中创建Web服务时,请指明   通过放置,通过该Web服务可用的方法   在 Public 方法的方法声明之前的WebMethod属性。   私有方法不能作为Web服务的入口点   虽然它们可以在同一个类中,但Web服务代码可以   打电话给他们。

答案 1 :(得分:1)

像这样更改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

data:"{username:'" + usernameVal + "',............}",
相关问题