gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:54:33 24 4
gpt4 key购买 nike

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

    <%@ 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>

最佳答案

您需要从引号中取出 data: "{'ntid': $('#Eid').val()}",

编辑 看看here ,您问题中的代码会正确尝试发送 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);
}
});

关于javascript - 将输入文本值传递给 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2604874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com