[英]How do i send string from code behind in C# to Ajax JQuery and how do i parse it?
i would like to know how to send a string from asp.net to the JQuery我想知道如何将字符串从 asp.net 发送到 JQuery
below is the code for the asp.net and the JQuery下面是 asp.net 和 JQuery 的代码
var jQueryXMLHttpRequest;
$(document).ready(function () {
readNamesSent();
});
//Method readNamesSent
//Parameters : string
//Retrun :
//Description : This file reads the name being sent from the StartingPage.aspx
function readNamesSent() {
jQueryXMLHttpRequest=$.ajax({
type: "POST",
url: "StartingPage.aspx/sendString",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
document.getElementById("status").innerHTML = response + "hi";
},
fail: function (response) {
}
});
}
Below is the asp.net file code.下面是 asp.net 文件代码。 The string that i am trying to send over to JQuery is the "name2" The main problem we are having is trying to send the value and establishing a connection.我试图发送到 JQuery 的字符串是“name2”我们遇到的主要问题是尝试发送值并建立连接。 JQuery to me is quite confusing. JQuery 对我来说很混乱。
Any help would be greatly appreciated!任何帮助将不胜感激!
public partial class StartingPage : System.Web.UI.Page
{
name in a string array over to jquerey
public void openFile()
{
// string LoadFile = "";
//Store the file name
List<string> list = new List<string>();
string fileStatus;
string[] fileNameListToBeSent;
string filepath = HttpContext.Current.Server.MapPath("MyFiles");
string filepath2 = HttpContext.Current.Server.MapPath("");
filepath2 = filepath2+@"\" + "MyFiles";
bool tof = Directory.Exists(filepath2);
fileNameListToBeSent = list.ToArray();
string name2 = string.Join("|", fileNameListToBeSent);
sendString(name2);
}
[WebMethod]
public static new string sendString(string names)
{
string returnData;
returnData = JsonConvert.SerializeObject(new { listOfName = names });
return reutrnData;
}
Please have a look into below URL and check if it suites your problem请查看下面的 URL 并检查它是否适合您的问题
https://www.aspsnippets.com/Articles/jQuery-AJAX-call-with-parameters-example-Send-parameters-to-WebMethod-in-jQuery-AJAX-POST-call.aspx https://www.aspsnippets.com/Articles/jQuery-AJAX-call-with-parameters-example-Send-parameters-to-WebMethod-in-jQuery-AJAX-POST-call.aspx
Refer the below sample请参阅以下示例
**Code**
[WebMethod]
public static string SayHello(string name)
{
return "Hello " + name;
}
**HTML**
<button type="button" onclick="callAJAX()" >Say Hello</button>
<script type="text/javascript">
function callAJAX() {
$.ajax({
type: "POST",
url: "Default.aspx/SayHello",
data:"{name: 'richie'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.