将输入文本值传递给ajax调用

我必须将输入文本中输入的字符串传递给通过jquery 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文件:

            
Employee's NTID:


你需要获取data: "{'ntid': $('#Eid').val()}",超出引号。

编辑看看这里 ,你问题中的代码正确地尝试发送ajax请求。

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

数据格式不正确。 您可以在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); } }); 

多谢你们! 它现在使用这一行:

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