user3739751
user3739751

Reputation: 31

AJAX call to a web service returns [object Object]

I am trying to call a webservice(URL) using jQuery and AJAX, the response from the webservice is a plain string, username and password are query parameters in webservice, this is the code I have:

<script>
    $(document).ready(function(){
        $(".signup").click(function(){
            $.ajax({  
           type: "GET",  
           url: "http://ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com:8080/UserManagement/rest/user_details/sign_in/?username=saurabh&password=hi", 
           dataType: "text",           
           success: function(resp){  
             // we have the response
             alert(resp);  
           },  
           error: function(e){  
             alert('Error121212: ' + e);  
           }  
         });
        });
    });
    </script>

I am getting error [object Object]

Why am I facing this error? Is there any error in the code or some thing more precise needsto be focused?Please help

Upvotes: 0

Views: 848

Answers (1)

Quentin
Quentin

Reputation: 944442

Look at the documentation:

error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

You are using the first argument (error: function(e){ alert('Error121212: ' + e);) so are trying to convert a jQuery XMLHttpRequest object to a string. It doesn't convert cleanly, so you get the standard result of translating a generic object to a string.

Look at the third argument instead.

Also consider examining the arguments are objects:

  • Without using the + operator (which will convert them to a string when the LHS is a string)
  • Without using alert (which will convert them to a string).

Such:

error: function(jqXHR, textStatus, errorThrown){  
    alert("There was an error. Look in the browser's JS console for details.");  
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
}  

You should probably check the JS console anyway, as it will probably give you a cross-origin error message. (Solutions for which are easily discoverable with a search engine).

Upvotes: 1

Related Questions