2014-10-08 5 views
0

프로젝트를 진행하면서 문제가 발생했습니다. 내 code.Below에서 문제가 무엇인지 찾아 낼 수 없다. Ajax url이 default.aspx의 ReceivedMessageByIndexNumber 함수를 가져올 수없는 이유를 모른다. 고맙습니다.Ajax를 통한 매개 변수 전달

자바 스크립트 :

ReceivedMessage(1); 

    function ReceivedMessage(indexNumber) 
    { 
     $.ajax({ 
      type: "Post", 
      url: "Default.aspx/ReceivedMessageByIndexNumber?indexNumber="+indexNumber, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       var data = response.d; 
       for (var i = 0; i < data.length; i++) { 
        alert(data[i]); 
       } 
      }, 
      failure: function (msg) { 
       $('#output').text(msg); 
      } 
     }); 
    } 

을 Default.aspx :

[WebMethod] 
    public static bool ReceivedMessageByIndexNumber(int textIndex) 
    { 
     string connectionString = @"Data Source=localhost;Initial Catalog=NotificationSystem;Integrated Security=True"; 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     using (SqlCommand command = connection.CreateCommand()) 
     { 
      connection.Open(); 
      command.CommandText = @"SELECT TextWord FROM TextProperty WHERE TextIndex = '" + textIndex + "'"; 
      command.ExecuteNonQuery(); 
      return true; 
     } 
    } 
+0

귀하의 쿼리 문자열 매개 변수는 서버 서버 (textIndex) –

+0

나는 그것을 그러나 다시 같은 문제 @에서 예상 이름으로 코드 (indexNumber)의 다른 이름이 메이저 MANN – user3817516

+0

내 대답이 당신에게 효과가 있었거나 무슨 일이 일어 났습니까? 작동했다면 답으로 표시 할 수 있습니까? 감사. – Carter

답변

0

이것은 HTTP POST 메서드이지만은 HTTP GET처럼 당신이 매개 변수를 전달하기 위해 노력하고 있습니다. HTTP GET과 같이 URL에 매개 변수를 지정할 수있는 위치를 본 적이 없다.

알림 아래에 JSON 데이터 행을 추가합니다. 그것이 게시물 요청을 채우는 것입니다.

data: '{textIndex: "' + indexNumber + '" }', 

전체 기능 : 당신이 요청 본문이 아닌 URL에 매개 변수를 추가 할 필요가 request.so

function ReceivedMessage(indexNumber) 
{ 
    $.ajax({ 
     type: "Post", 
     url: "Default.aspx/ReceivedMessageByIndexNumber", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: '{textIndex: "' + indexNumber + '" }', 
     success: function (response) { 
      var data = response.d; 
      for (var i = 0; i < data.length; i++) { 
       alert(data[i]); 
      } 
     }, 
     failure: function (msg) { 
      $('#output').text(msg); 
     } 
    }); 
} 
+0

이 기능을 사용하셨습니까? 그렇다면 답으로 표시하는 것이 좋습니다. – Carter

0

당신은 게시물을 수행하고 있습니다. 그래서 당신의 아약스 호출 변경

function ReceivedMessage(indexNumber) 
    { 
     $.ajax({ 
      type: "Post", 
      url: "Default.aspx/ReceivedMessageByIndexNumber?indexNumber="+indexNumber, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      data: "{'textIndex': " + indexNumber+ "}", 
      success: function (response) { 
       var data = response.d; 
       for (var i = 0; i < data.length; i++) { 
        alert(data[i]); 
       } 
      }, 
      failure: function (msg) { 
       $('#output').text(msg); 
      } 
     }); 
    } 
0
function ReceivedMessage(indexNumber) 
{ 
    $.ajax({ 
     type: "Post", 
     url: "/Default/ReceivedMessageByIndexNumber/", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: { textIndex: indexNumber }, 
     success: function (response) { 
      var data = response.d; 
      for (var i = 0; i < data.length; i++) { 
       alert(data[i]); 
      } 
     }, 
     failure: function (msg) { 
      $('#output').text(msg); 
     } 
    }); 
} 
관련 문제