0

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.

1
  • You will probably get alot of information if you check the server response in the console or net tab i firebug. Commented Apr 8, 2011 at 12:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

9 Comments

The second argument passed to the error function is a text version of the error.
The result of this is: Error- Status: parsererror jqXHR Status: 200
Sounds like malformed json data, try data: "{'auktionid':'" + test+ "' }"
@Soeren - Can you try my code in the 'Updated Example' in my edited post. I have added your web method to the ajax call and passed the parameters in the data
Still the same. I tried using jqXHR.statusText instead of jqXHR.responseText, and the error now looks like this: Error- Status: parsererror jqXHR Status: 200 jqXHR statusText Text:OK
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.