我正在简单地调用Webmethod(Pagemethod),但我一直收到这个错误:
[object XMLHttpRequest]
使用Javascript:
var id = $('#' + this.Div).attr('id');
var test = $('#' + id).parent('.Prod-top-time').prev().attr('id');
test = test.replace('navn_', '');
var parameters = {'auktionid': test};
$.ajax({
type: "POST",
url: "Default.aspx/AuctionEnd",
data: JSON.stringify(parameters),
//data: JSON.stringify({ auktionid: 34}),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function(ret)
{
if (ret.hasOwnProperty('d'))
stuff(ret.d);
else
stuff(ret);
}
});
function stuff(msg) {
alert(msg);
}
在第一部分中,我从div id中提取一个值。这是一个用作参数的数字。
Webmethod就像这样简单:(仅用于测试)
[WebMethod]
public static string AuctionEnd(int auktionid)
{
return auktionid.ToString();
}
无论我抛出什么,它都会回归那个错误。
答案 0 :(得分:1)
我可以在隧道尽头找到一盏灯 如果要在jquery中使用WebMethod,则必须将此标记添加到web.config
<configuration>
<system.web>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
</configuration>
祝你好运
答案 1 :(得分:0)
您正在消息框中显示错误对象。错误函数返回如下:
error(jqXHR, textStatus, errorThrown)Function
请求失败时要调用的函数。该函数接收三个参数:jqXHR(在jQuery 1.4.x,XMLHttpRequest中)对象,描述发生的错误类型的字符串和可选的异常对象(如果发生)。第二个参数的可能值(除了null)是“timeout”,“error”,“abort”和“parsererror”。这是一个Ajax事件。从jQuery 1.5开始,错误设置可以接受一系列函数。每个函数将依次调用。注意:不会为跨域脚本和JSONP请求调用此处理程序。
您需要显示jqXHR对象的详细信息。
jqXHR对象
jQuery 1.5中$ .ajax()返回的jQuery XMLHttpRequest(jqXHR)对象是浏览器的原生XMLHttpRequest对象的超集。例如,它包含responseText和responseXML属性,以及getResponseHeader()方法。当传输机制不是XMLHttpRequest(例如,JSONP请求的脚本标记)时,jqXHR对象尽可能模拟本机XHR功能。
所有信息均在
http://api.jquery.com/jQuery.ajax/
我使用错误处理程序的示例ajax调用是:
//Call the approve method on the code behind
$.ajax({
type: "POST",
url: ResolveUrl("~/Pages/Mobile/Login.aspx/LoginUser"),
data: "{'Username':'" + $Username + "', 'Password':'" + $Password + "' }", //Pass the parameter names and values
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
error: function (jqXHR, textStatus, errorThrown) {
alert("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText) },
success: function (msg) {
if (msg.d == true) {
window.location.href = ResolveUrl("~/Pages/Mobile/Index.aspx");
}
else {
//show error
alert('login failed');
}
}
});
注意错误处理程序
的区别更新示例:
//Call the approve method on the code behind
$.ajax({
type: "POST",
url: "Default.aspx/AuctionEnd",
data: "{'auktionid':'" + test+ "'}", //Pass the parameter name and value
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
error: function (jqXHR, textStatus, errorThrown) {
alert("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText) },
success: function (msg) {
if (msg.d == true) {
alert('success');
}
else {
//show error
alert('failed');
}
}
});