[英]Passing string from AJAX to C# code behind
I've looked at so many comments on this subject, and I have tried a number of suggested methods for accomplishing this but I'm still not able to access the code behind. 我已经看过很多关于此主题的评论,并且我尝试了一些建议的方法来实现此目的,但是我仍然无法访问后面的代码。 Error message comes back with the success routine.
成功例程返回错误消息。 I am using a Select2 as an input source (there is no list, only what the operator desires to enter) for multiple entries to be passed via ajax to my code behind.
我将Select2用作输入源(没有列表,只有操作员希望输入的内容),以便将多个条目通过Ajax传递到我后面的代码中。 I believe the data type may not be the same as the code behind but not sure how to define it other than what I have, or has it been depracated and I am not aware.
我相信数据类型可能与后面的代码不同,但是除了我拥有的内容之外,不确定如何定义它,或者不确定该数据类型并且我不知道。 I changed to the original programming when the error occurred.
发生错误时,我更改为原始编程。 myData is changed to model to match code behind.
将myData更改为模型以匹配后面的代码。 Here is what I get in the select2 if I put in "test" and "copy".
这是我输入“ test”和“ copy”后在select2中得到的结果。
After model value is: test,copy
模型值后为:test,copy
Here is my Client side code: 这是我的客户端代码:
...
<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>
Here is the code behind: 这是背后的代码:
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";
}
}
}
Here is the error message: 这是错误消息:
success!
成功! {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
{“消息”:“验证失败。”,“ StackTrace”:null,“ ExceptionType”:“ System.InvalidOperationException”}
The program says it is successful but gives an error that the value is null being returned (or sent) but on the code behind I have a breakpoint at the System.Diagnostics.Debug.WriteLine... line and it never reaches there. 该程序说它成功了,但是给出了一个错误,即返回(或发送)该值为null,但是在后面的代码上,我在System.Diagnostics.Debug.WriteLine ...行有一个断点,但从未到达那里。 Any help would be appreciated.
任何帮助,将不胜感激。
In your code behind 在你的代码后面
public partial class _Default : Page
{
string myStrCb = "Value from back";
protected void Page_Load(object sender, EventArgs e)
{
//... any code
}
// ...more code
}
In your front code 在前面的代码中
<script>
var myJsVar = <%=myStrCb %>
</script>
Use in script 在脚本中使用
function WriteValue()
{
console.log(myJsVar); // Value from back
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.