【发布时间】:2016-12-22 11:40:27
【问题描述】:
我想在单击按钮时在我的列表框中添加项目,然后,它必须通过ajax返回已添加到列表框中的值。
这里是代码,我试过了。
$('#right').click(function () {
alert("Start process");
var item = "testing";
$.ajax({
url: 'ReportSalesAll.aspx/setRightListBoxitems',
method: 'post',
ContentType: 'application/json',
data: '{listItems:' + item + '}',
dataType: 'json',
success: function (data) {
alert("result = " + data.d.text)
},
failure: function (response) {
alert(response.d);
},
error: function (error) {
alert("Error = "+error);
}
});
});
aspx代码:
<input type="button" id="right" value=">>" />
<asp:ListBox ID="lstRight" runat="server" SelectionMode="Multiple" Width="100%" Height="220"></asp:ListBox>
后端代码:
[System.Web.Services.WebMethod]
public static string setRightListBoxitems(string listItems)
{
ReportSalesAll rs = new ReportSalesAll();
rs.lstRight.Items[0].Text = listItems;
rs.lstRight.Items[0].Value = "faisal "+listItems ;
return rs.lstRight.Items[0].Text;
}
运行
Error = [Object object]时出现错误就绪状态:4, 状态:500 statusText:内部服务器错误 responseText:消息:“无效的 JSON 原语:测试。”,StackTrace“:”在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
【问题讨论】:
-
而不是
alert("Error = "+error);使用console.log("Error:", error);- 这样您就可以实际看到服务器返回的错误响应。 -
你试过在浏览器中调试你的代码吗?这可以为您提供错误及其对象
-
@mpf82 现在我已经更新了。请检查错误
-
@NMathur 请立即查看
-
服务器是否以有效的 JSON 响应?您在
'applciation/json'中也有错字。
标签: javascript c# jquery ajax