asp.net ajax webmethod 返回 [object XMLHttpRequest]

发布于 2024-10-31 11:14:48 字数 1326 浏览 1 评论 0原文

我正在对 Webmethod (Pagemethod) 进行简单调用,但不断收到此错误:

[对象 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();

}

无论我向它抛出什么,它都会返回该错误。

I'm making a simple call to a Webmethod (Pagemethod), but I keep getting this error:

[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);
                             }

In the first part, I extract a value from a div id. This is a number used as parameter.

The Webmethod is as simple as this: (Just for testing so far)

[WebMethod]
public static string AuctionEnd(int auktionid)
{
    return auktionid.ToString();

}

No matter what I throw at it, it returnes that error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-11-07 11:14:48

我能在隧道尽头找到一盏灯
当你想在 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>

祝你好运

i could find a light at the end of the tunnel
when you want use WebMethod in jquery , you must add this tag to 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>

good luck

沙与沫 2024-11-07 11:14:48

您正在消息框中显示错误对象。错误函数返回如下:

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');
                }
            }
        });

You are showing the error object in the message box. The error function returns as follows:

error(jqXHR, textStatus, errorThrown)Function 

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". This is an Ajax Event. As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests.

you need to show the details of the jqXHR object.

The jqXHR Object

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method. When the transport mechanism is something other than XMLHttpRequest (for example, a script tag for a JSONP request) the jqXHR object simulates native XHR functionality where possible.

All information is at

http://api.jquery.com/jQuery.ajax/

And example ajax call i use with error handler is:

        //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');
                }
            }
        });

Note the difference in the error handler

Updated Example:

        //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');
                }
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文