[英]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.