Vikas
Vikas

Reputation: 1101

Jquery Ajax error

I am trying to do an ajax Call using jquery in one of my webpages.

It's going into Error function all the time. When I look at the console in Firebug, the status seems to be ok , it displays 200 OK.

And when I manually launch the url in a firefox window, i am getting the expected page.

But when the same url is being called through jquery ajax, it always goes into the error function.

Following are the various values that i got during debug xhr.readyState=4 xhr.statusText=Error xhr.responseBody=undefined.

Could anyone of you please help me with this ?

function ajax_call(urlString)
        {
            ret_val="";
            $.ajax
            (
                {
                    type: "GET",
                    url: urlString,
                    async:false,
                    cache:false,
                    success: function(msg)
                    {
                        ret_val=msg;
                    },
                    error:function (xhr, textStatus, thrownError)
                    {
                        ret_val=xhr.readyState;
                        alert("textStatus=" +textStatus);
                    }
                }
            );
            return ret_val;
        } 

Upvotes: 0

Views: 1443

Answers (2)

Vikas
Vikas

Reputation: 1101

It was my mistake !

In the browser URL , i was looking at "http://localhost/data.php"

in the AJAX call , i was calling the same php by giving absolute url like "http://xyz.com/data.php"

as a result, ajax call was considering this as a cross domain call, although it was for the same php.

Resolved this by calling the relative url "/data.php"

Upvotes: 0

tjm
tjm

Reputation: 7550

It looks like you might be returning plaintext from your call, looking at the jQuery docs for ajax it says the dataType parameter defaults to:

dataType                                         String
Default: Intelligent Guess (xml, json, script, or html)

That seems to imply that it won't "guess" at plaintext, so maybe try adding

dataType: 'text'

(assuming you are returning plaintext - if not I'll delete this) to your parameters.


Also, just to confirm, the URL you are requesting is on the same server as the javascript making the request, isn't it?

Upvotes: 1

Related Questions