[英]Passing string from AJAX to ASP.NET C# code behind
我是JS的新手,即使对AJAX经验不足。 我只是想将一个值传递给后面的代码并返回扩展的响应。 问题是,尽管调用成功,但从AJAX传递到C#的字符串值只不过是“ undefined”,这使我发疯。
JS
function test() {
var message = "this is a test" ;
Display(message);}
function Display(words) {
var hummus = { 'pass': words};
$.ajax({
type: 'POST',
url: 'Account.aspx/ShowMe',
data: hummus,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
alert("Your fortune: " + response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + words + "\n\nError: " + lion);
}
});}
背后的代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ShowMe(string pass)
{
string beans = pass + ". We repeat this is only a test.";
return beans;
}
最终结果总是“您的运势:不确定。我们重复这仅是一次考验”。 我只想知道我在想什么。 是的,这可能是一个愚蠢的问题,但我的搜索没有任何帮助。
您的问题是,您正在尝试在方法的public static string ShowMe(string pass)
接受一个字符串,但是您正在将JavaScript对象作为数据传递。 看看何时进行Ajax调用ASP.Net会尽力将发布的数据与参数(称为模型绑定)中的类型进行匹配。 如果无法实现,则会传入一个null。
因此,在您的JavaScript中,您将使用以下方法传递JavaScript对象:
var hummus = { 'pass': words};
$.ajax({
....,
....,
data: hummus,
如果要发布对象,则您的控制器/方法需要具有JS绑定到的C#模型(类)。
因此,更改您的方法以接受模型:
// somewhere in your code put this little model
// this is what your JavaScript object will get bound to when you post
public MyModel{
// this is to match the property name on your JavaScript object, its case sensitive i.e { 'pass': words};
public string pass {get;set;}
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ShowMe(MyModel model)
{
// you can now access the properties on your MyModel like this
string beans = model.pass + ". We repeat this is only a test.";
return beans;
}
我在您的代码中发现了两个问题-
data: JSON.stringify(hummus),
function test() {
var message = "this is a test";
Display(message);
}
function Display(words) {
var hummus = { 'pass': words };
$.ajax({
type: 'POST',
url: 'Account.aspx/ShowMe',
data: JSON.stringify(hummus), // Missing JSON.stringify
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
alert("Your fortune: " + response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// remove lion which variable doesn't exist.
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + words);
}
});
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Account.aspx.cs" Inherits="WebApplication1.Account" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<button type="button" onclick="test()">Display Word</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function test() {
var message = "this is a test";
Display(message);
}
function Display(words) {
var hummus = { 'pass': words };
$.ajax({
type: 'POST',
url: 'Account.aspx/ShowMe',
data: JSON.stringify(hummus), // Missing JSON.stringify
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
alert("Your fortune: " + response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// remove lion which variable doesn't exist.
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + words);
}
});
}
</script>
</form>
</body>
</html>
public partial class Account : System.Web.UI.Page
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string ShowMe(string pass)
{
string beans = pass + ". We repeat this is only a test.";
return beans;
}
}
谢谢你们的帮助。 我将尝试使用Model方法,JSON.stringify是我之前尝试并最终成功的方法。
显然问题是我不了解我的浏览器是如何工作的。 无论我做了什么更改,我都会遇到相同的错误。 原因?
我的代码不在页面上的标签中,而是在一个单独的js文件中,Chrome正在为我缓存该文件,有效地抵消了我为尝试解决该问题所做的所有更改,并在此过程中使我发疯。
总之,我已经学到了一种新的技术,即模型绑定,并且代码的位置可以极大地影响您的Javascript体验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.