[英]Passing string from AJAX to C# code behind
我已經看過很多關於此主題的評論,並且我嘗試了一些建議的方法來實現此目的,但是我仍然無法訪問后面的代碼。 成功例程返回錯誤消息。 我將Select2用作輸入源(沒有列表,只有操作員希望輸入的內容),以便將多個條目通過Ajax傳遞到我后面的代碼中。 我相信數據類型可能與后面的代碼不同,但是除了我擁有的內容之外,不確定如何定義它,或者不確定該數據類型並且我不知道。 發生錯誤時,我更改為原始編程。 將myData更改為模型以匹配后面的代碼。 這是我輸入“ test”和“ copy”后在select2中得到的結果。
模型值后為:test,copy
這是我的客戶端代碼:
...
<select id="SearchSelect" style="width: 150px" name="SearchSelct"
multiple onmouseover="SearchTip('#SrchToolTip','#hdnviewsrch');return
false;"></select>
<textarea id="SrchToolTip" style="color:blue" hidden="hidden">You can
enter any keyword you like.</textarea>
<br />
<br />
<asp:Button ID="SearchFnd" runat="server" Text="Submit"
OnClientClick="SearchSel('#SearchSelect');return false;"
ValidateRequestMode="Disabled" UseSubmitBehavior="False"/>
<script type="text/javascript">
$(document).ready(function () {
//On client side - Here we render the Select 2
$("#SearchSelect").select2(
{
placeholder: "Enter Search Parameters",
tags: true,
tokenSeparators: [','],
allowClear: true,
minimumInputLength: 1,
width: 'resolve',
});
});
$("#SearchSelect").on("change", function (e) {
SearchChange("SearchSelect"); });
function SearchTip(SrchElem) {
$(SrchElem).show();
console.log("Search Tip value is: " + $("#SearchSelect").val());
};
function SearchChange(name, evt) {
console.log("Search Change value is: " + $("#SearchSelect").val()
+ "; Name: " + name);
};
function SearchSel(SchElem) {
var model= { "SrchData": $(SchElem).val() };
$.ajax(
{
type: "POST",
url: '<%= ResolveUrl("~/Default.aspx/SearchFind") %>',
data: JSON.stringify(model.SrchData),
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (result) {
alert('success');
console.log("Search data obtained: " + result);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error: " + errorThrown);
},
failure: function () {
alert('Failure');
}
processResults: function (data, params) {
console.log("Search data obtained: " + data);
return {
results: data.d,
pagination: {
more: data.more
}
};
}
});
}
</script>
這是背后的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
namespace WebApplication2
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Schdata
{
public string SrchData { get; set; }
}
[WebMethod()]
// [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string SearchFind(Schdata model)
{
System.Diagnostics.Debug.WriteLine("Went into the SearchFind
subroutine " + model.SrchData);
return "1";
}
}
}
這是錯誤消息:
成功! {“消息”:“驗證失敗。”,“ StackTrace”:null,“ ExceptionType”:“ System.InvalidOperationException”}
該程序說它成功了,但是給出了一個錯誤,即返回(或發送)該值為null,但是在后面的代碼上,我在System.Diagnostics.Debug.WriteLine ...行有一個斷點,但從未到達那里。 任何幫助,將不勝感激。
在你的代碼后面
public partial class _Default : Page
{
string myStrCb = "Value from back";
protected void Page_Load(object sender, EventArgs e)
{
//... any code
}
// ...more code
}
在前面的代碼中
<script>
var myJsVar = <%=myStrCb %>
</script>
在腳本中使用
function WriteValue()
{
console.log(myJsVar); // Value from back
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.