【问题标题】:Ajax webmethod in asp.net problemsasp.net中的ajax webmethod问题
【发布时间】:2012-11-27 05:52:31
【问题描述】:

这是我的 jquery 函数。我有两个功能。 showgrid() 函数将数据加载到 html 表。从那如果我editrow() funcion。我遇到了错误。

function showgrid() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "CourseSearch.aspx/DisplayCourses",
        data: "{'strSearchText':'" + $("#txtSearchText").val() + "'}",
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.d.length; i++) {
            //$("#searchCoursesDiv").append("<tr><td style='color:#000888;'>" + data.d[i].CourseName + "</td><td>|</td><td style='color:#000888;'>" + data.d[i].CourseCode + "</td></tr><tr><td border='0px' colspan='3'><hr/></td></tr>");
                $("#searchCoursesDiv").append("<table><tr><td align='left' width='500px' style='color:#000888;' colspan='3'>" + data.d[i].CourseName + " | " + data.d[i].CourseCode + "</td></tr><tr><td colspan='3' valign='bottom'><hr /></td></tr><tr><td align='left'>Description:</td><td></td><td rowspan='2' align='right'><a href='#'>Course Details...</a><br/><a href=# onclick='EditRow( " + data.d[i].CourseCode + ")'>Nominate as Trainer...</a></td></tr><tr><td></td><td></td></tr><table>");
            }
        },
        error: function (result) {
                    alert("Error");
                }
    });
    // });
    return false;
}

//This function returns coursecode when user click html table row,.. this function get value from showgrid function and return value to page.but this funcion is not firing.i m getting error when click editrow() funciont in showgrid funcion.

function EditRow(CourseCode) {
    alert(CourseCode);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "CourseSearch.aspx/StoreCourseCode",
        data: "{'CourseCode':'" + CourseCode + "'}",
        dataType: "json",
        success: function (data) {
            //                    $("#displayasstname").html(data.d);
            window.location = "nextpage.aspx";
        },
        error: function (result) {
            alert("Error");
        }
    });                     
}

这是我的 HTML 设计代码:

<html>
    <body>
        <div id="searchCoursesDiv">
        </div>
    </body>
</html>

问题:当我单击editrow() 函数时,我收到类似coursecode 'DOTNET3.0' undefined. 的错误

【问题讨论】:

    标签: jquery asp.net ajax webmethod


    【解决方案1】:

    删除帖子中的 json 字符串...像这样

    data: {"CourseCode": CourseCode },
    

    改为

    data: "{'CourseCode':'" + CourseCode + "'}",
    

    【讨论】:

      【解决方案2】:

      您可以使用JSON.stringify 来管理您的参数:

      var data = JSON.stringify({ CourseCode: CourseCode, ... });
      

      然后:

      function EditRow(CourseCode) {
      
              var data = JSON.stringify({ CourseCode: CourseCode, ... });
                  $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: "CourseSearch.aspx/StoreCourseCode",
                      data: data,
                      dataType: "json",
                      success: function (data) {
                          //                    $("#displayasstname").html(data.d);
                          window.location = "nextpage.aspx";
      
                      },
                      error: function (result) {
                          alert("Error");
                      }
                              });                     
              }
      

      【讨论】:

        猜你喜欢
        • 2012-09-21
        • 2012-10-28
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 2018-11-08
        相关资源
        最近更新 更多