我想知道如何將字串從 asp.net 發送到 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) {
}
});
}
下面是asp.net檔案代碼。我試圖發送給 JQuery 的字串是“name2”我們遇到的主要問題是嘗試發送值并建立連接。JQuery 對我來說很混亂。
任何幫助將不勝感激!
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;
}
uj5u.com熱心網友回復:
請查看以下 URL 并檢查它是否適合您的問題
https://www.aspsnippets.com/Articles/jQuery-AJAX-call-with-parameters-example-Send-parameters-to-WebMethod-in-jQuery-AJAX-POST-call.aspx
uj5u.com熱心網友回復:
請參考以下示例
**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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373263.html
