【发布时间】:2015-09-05 15:22:48
【问题描述】:
我正在尝试为此按钮的点击事件调用代码隐藏方法:
<input type="button" id="btn_submit1" class="btn btn-default" value="Calculate" />
这里是它的 AJAX 调用:
$(document).ready(function() {
$("#btn_submit1").click(function() {
var msg = $("#txt_1_1").val();
$.ajax({
type: 'POST',
url: 'Calc.aspx/calculate',
data: '{sText:' + msg + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(response) {
alert("success!!");
alert(response.d);
$("#test").html(response.d);
},
failure: function(response) {
alert(response.d);
}
});
});
});
这是 WebMethod:
[System.Web.Services.WebMethod]
public static string calculate(string sText)
{
System.Diagnostics.Debug.WriteLine("sText = " + sText);
return sText;
}
成功部分的两个警报都被调用,但第二个警报显示“未定义”,因此 div“测试”中没有显示任何内容。 我搜索了很多,但找不到代码有什么问题。
此外,Debug.WriteLine 不会在“输出”窗口中打印任何内容。在网上搜索后使用了这个,我不确定它是否是正确的打印方式。
【问题讨论】:
-
大概然后
response.d是未定义的。response中有什么内容?为什么你期望它有一个d属性? -
"Debug.WriteLine does not print anything in the Output window"- 什么输出窗口?这是一个 Web 应用程序,它们通常没有与之关联的控制台窗口。 -
@Archer - console.log(data) 在 Chrome 控制台中显示此错误 - Uncaught ReferenceError: data is not defined 但是 console.log(msg) 显示我在文本框“txt_1_1”中的所有内容。
-
对不起 - 我的错(习惯的力量)。将其更改为
console.log(response);它会准确显示返回的内容,而不是 [object Object] -
@Archer - 这是 console.log(response) 控制台中的消息: Object {Message: "Authentication failed.", StackTrace: null, ExceptionType: "System.InvalidOperationException"}
标签: javascript jquery asp.net ajax web-services