2012-11-27 83 views
1

這是我的jquery函數。我有兩個功能。 showgrid()函數將數據加載到html表中。從那如果我editrow()功能。我得到錯誤。asp.net中的Ajax webmethod問題

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.

回答

1

刪除後您的JSON字符串。 .. like this

data: {"CourseCode": CourseCode }, 

instead

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

可以使用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"); 
       } 
         });      
     }