2010-04-09 104 views
1

我必須將輸入文本中輸入的字符串傳遞給通過jquery ajax調用的服務器方法。但它沒有經過。可以請有人告訴我我在這裏做錯了什麼。下面是代碼:將輸入文本值傳遞給ajax調用

$.ajaxSetup({ cache: false timeout: 1000000}); function concatObject(obj) { strArray = []; //new Array for (prop in obj) {  strArray.push(prop + " value :" + obj[prop]); } return strArray.join();} //var Eid = "stephen.gilroy1"; function testCAll() { //var ntid = $('#Eid').val();  $.ajax({  type: "POST",  url: "Testing.aspx/SendMessage",  //data: "{'ntid':'stephen.gilroy1'}",  //working  data: "{'ntid': $('#Eid').val()}",  contentType: "application/json; charset=utf-8",  dataType: "json",  success: function(result) {   alert(result.d);   resultData = eval("(" + result.d + ")");   $("#rawResponse").html(result.d);   //$("#response").html(resultData.sn);  },  error: function(result) {   alert("jQuery Error:" + result.statusText);  } });}$.ajaxSetup({ 
    cache: false 
    //timeout: 1000000 
}); 
function concatObject(obj) { 
    strArray = []; //new Array 
    for (prop in obj) { 
     strArray.push(prop + " value :" + obj[prop]); 
    } 
    return strArray.join(); 
} 

//var Eid = "stephen.gilroy1"; 

function testCAll() { 
    //var ntid = $('#Eid').val(); 
    $.ajax({ 
     type: "POST", 
     url: "Testing.aspx/SendMessage", 
     //data: "{'ntid':'stephen.gilroy1'}",  //working 
     data: "{'ntid': $('#Eid').val()}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(result) { 
      alert(result.d); 
      resultData = eval("(" + result.d + ")"); 
      $("#rawResponse").html(result.d); 
      //$("#response").html(resultData.sn); 
     }, 
     error: function(result) { 
      alert("jQuery Error:" + result.statusText); 
     } 
    }); 
} 

以上是js文件和下面是它的aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Testing.aspx.cs" Inherits="Testing" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="jquery.js" type="text/javascript"></script> 

    <script src="Testing.js" type="text/javascript"></script> 

    <script src="json2.js" type="text/javascript"></script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    Employee's NTID: <input type="text" id = "Eid" name="Employee_NTID" /> 
     <asp:GridView ID="GridView1" runat="server"> 
     </asp:GridView> 
     <br /> 
<br /> 
    <input type="button" onclick="testCAll()" value = "Search"/> 

     <div id="rawResponse"></div> 
     <hr /> 
     <div id="response"></div> 

    </div> 

    </form> 
</body> 
</html> 

回答

2

你需要採取data: "{'ntid': $('#Eid').val()}",出報價。

編輯看看here,在你的問題的代碼正確地嘗試發送ajax請求。

EDIT 2在這裏你去:

$.ajax({ 
    type: "POST", 
    url: "Testing.aspx/SendMessage", 
    data: { ntid: $('#Eid').val()}, // Notice the lack of quotes 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(result) { 
     console.log(result.d); 
     resultData = eval("(" + result.d + ")"); 
     //$("#rawResponse").html(result.d); 
    }, 
    error: function(result) { 
     alert("jQuery Error:" + result.statusText); 
    } 
}); 
+0

謝謝,但問題仍然在這裏 – amby 2010-04-09 03:12:24

+0

@amby:具體是什麼不工作。正如我提供的鏈接所顯示的那樣,AJAX調用正在正確觸發。 – R0MANARMY 2010-04-09 04:12:59

+0

我知道,我的代碼成功發送Ajax請求,因爲我試過只發送簡單的字符串像 數據:「{‘NTID’:‘stephen.gilroy1’}」, 它工作得很好,但是當我嘗試發送輸入文本框字符串,然後它給了jQuery:內部服務器錯誤 – amby 2010-04-09 04:13:12

0

數據沒有格式化的權利。您可以在ajax()函數之外創建json對象,以便於管理。

var keyvalue = { 
     ntid : $('#Eid').val() 
    }; 
    $.ajax({ 
      type: "POST", 
      url: "Testing.aspx/SendMessage", 
      //data: "{'ntid':'stephen.gilroy1'}",  //working 
      data: keyvalue, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(result) { 
       alert(result.d); 
       resultData = eval("(" + result.d + ")"); 
       $("#rawResponse").html(result.d); 
       //$("#response").html(resultData.sn); 
      }, 
      error: function(result) { 
       alert("jQuery Error:" + result.statusText); 
      } 
    }); 
+0

謝謝,但仍然沒有解決:(...任何其他想法 – amby 2010-04-09 04:04:28

+0

你得到一個javascript錯誤? – 2010-04-09 04:22:32

+0

jquery內部服務器錯誤,但事情是,當我嘗試發送這樣的數據:數據:「{'ntid': 'stepqwe1'}「,它的工作原理是當嘗試發送輸入文本值時,那麼它給出上述錯誤 – amby 2010-04-09 14:53:53

0

謝謝你們! 其現在的工作,通過使用這一行:

data: "{'ntid': '"+$('#Eid').val()+"'}",