Dmitry Belaventsev
Dmitry Belaventsev

Reputation: 6657

Receive serialized in php data by using ajax

I have a php script, which return serialized in php data. And I try to receive this data by using $.ajax() method from jQuery 1.7. Here is the example.

$.ajax({
    url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
    type: 'GET',
    dataType: 'text',
    cache: 'false',
    complete: function(res) {
        alert('COMPLETE() done');
        console.log(res);
    }
});

In console I see only

Object { readyState=0, status=0, statusText="error"}

So, what I do wrong? Could you help me please?

UPD

Interesting notice: if I use JSONP dataType request can receive data, but can't process it. Here is an example.

$.ajax({
    url: 'http://input.name/get.php?do=lookup',
    data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
    dataType: 'jsonp',
    cache: false,
    success: function(data) {
        alert("Data: "+data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert("Error: "+textStatus);
        console.log(jqXHR);
    }
});

Upvotes: 0

Views: 210

Answers (3)

Ayman Safadi
Ayman Safadi

Reputation: 11552

Your code is probably fine, but you're trying to violate the same origin policy. Basically, if your site is http://aaa.com/, you cannot make AJAX called to http://bbb.com/.

There are a few ways around it:

But most of them require that both sides play nice.

Upvotes: 2

Thomas Clayson
Thomas Clayson

Reputation: 29925

Instead of complete: use success: then res will be the returned data from your ajax request.

Remember to use error: as well incase there is an error with you call, as it seems that there might be in your console output.

Code:

$.ajax({
    url: 'http://input.name/get.php?do=lookup',
    data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
    cache: false,
    success: function(data) {
        alert("Data: "+data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert("Error: "+textStatus);
        console.log(jqXHR);
    }
});

Upvotes: 2

kaz
kaz

Reputation: 1943

The response is the second parameter of complete function:

$.ajax({
    url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
    type: 'GET',
    dataType: 'text',
    cache: 'false',
    complete: function(res,response) {
        alert('COMPLETE() done');
        console.log(response);
    }
});

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

You should also consider using JSON, not php serialized data

Upvotes: 1

Related Questions