2014-10-08 278 views
0

我正在研究項目並面臨問題。無法找到我的代碼中有什麼問題。以下是我的代碼,不知道爲什麼Ajax url無法獲取Default.aspx的ReceivedMessageByIndexNumber函數。謝謝。通過Ajax傳遞參數

的JavaScript:

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) – 2014-10-08 14:00:34

+0

我這樣做,但再次同樣的問題@代碼(indexNumber)來命名有不同的名稱主要曼 – user3817516 2014-10-08 14:02:13

+0

我的答案是否適合您或發生了什麼?如果它確實有效,你可以將它標記爲答案?謝謝。 – Carter 2014-10-09 18:37:38

回答

0

這是一個HTTP POST方法,但你想傳遞參數就像是一個HTTP GET。我從來沒有見過它寫在哪裏你可以指定URL中的參數像HTTP GET。

注意我在下面添加了JSON數據行。這是填寫您的發佈請求。

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

整函數:

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 2014-10-08 19:49:26

0

request.so你需要在請求主體,而不是URL添加參數您正在執行職務。所以改變你Ajax調用

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); 
     } 
    }); 
}