Javascript 将输入文本值传递给ajax调用
我必须通过jQueryAjax将输入文本中输入的字符串传递给服务器方法调用。但它没有通过。谁能告诉我我做错了什么吗。代码如下:Javascript 将输入文本值传递给ajax调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我必须通过jQueryAjax将输入文本中输入的字符串传递给服务器方法调用。但它没有通过。谁能告诉我我做错了什么吗。代码如下: $.ajaxSetup({ cache: false timeout: 1000000}); function concatObject(obj) { strArray = []; //new Array for (prop in obj) { strArray.push(prop + " value :" + obj[prop])
$.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>
员工的NTID:
您需要从引号中提取数据:“{'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()+"'}",
谢谢,但问题仍然存在here@amby:什么具体不起作用。正如我提供的链接所示,AJAX调用被正确触发。我知道我的代码成功地发送了AJAX请求,因为我尝试只发送简单的字符串数据:“{'ntid':'stephen.gilloy1'}”,它工作得很好,但当我尝试发送输入文本框字符串时,它会给出jquery:internalservererror@amby:你看过我提供的链接了吗?您还应该更新问题中的代码,以反映我和Jonathan Mayhak关于使用对象而不是字符串作为数据参数的评论?我在那里看不到您的代码。jquery内部服务器错误,但问题是当我尝试发送这样的数据时:“{'ntid':'stepqwe1'}”,它可以工作,但当尝试发送输入文本值时,它给出了上述错误