0

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);
    }
});
Dmitry Belaventsev
  • 6,347
  • 12
  • 52
  • 75

3 Answers3

2

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);
    }
});
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
2

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.

Community
  • 1
  • 1
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41
  • but if it's the problem - why I don't receive any detailed error message from jQuery? – Dmitry Belaventsev Jan 04 '12 at 12:31
  • 1
    I'm not sure what you'r expecting, but *I* do see the following error message in my JS console: `XMLHttpRequest cannot load http://input.name/get.php?do=lookup&domain=twittorama&tlds=.ru,.com,.net,.comf.ru. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.` – Ayman Safadi Jan 04 '12 at 12:33
  • I've updated my answer - in dataType='jsonp' case request can receive the data... – Dmitry Belaventsev Jan 04 '12 at 12:43
  • Because it's not JSON. That's what I meant by both sides "playing nice". – Ayman Safadi Jan 04 '12 at 12:46
  • I just want to say that it's not a policy problem if I see the data in console. So, I don't understand why I can't see the data and receive an error when I use 'text' dataType... – Dmitry Belaventsev Jan 04 '12 at 13:00
  • You can't the see data when using `text` because of the same origin policy. You can't use the data using `jsonp` because the data is not JSON. They're two completely different issues. – Ayman Safadi Jan 04 '12 at 13:05
  • so, it means that for 'jsonp' case origin policy is right, but for 'text' case it's not right. I always think that ways to get data by using different data types are the same - but processing of this data is unique for each case. this fact embarrass me. – Dmitry Belaventsev Jan 04 '12 at 13:14
1

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

kaz
  • 1,943
  • 1
  • 13
  • 19
  • here is some problem - input.name is a service site, which provide only php-serialized data – Dmitry Belaventsev Jan 04 '12 at 12:23
  • I see this function. But before process this data, I should get it from remote host. I try to use your code - it logs "error" in console. – Dmitry Belaventsev Jan 04 '12 at 12:25
  • try using your browser console for debugging this. You should have more information about this request in firebug plugin (firefox) or developer console (chrome) - there are also detailed information about each ajax request – kaz Jan 04 '12 at 12:33
  • propably it's the same origin policy violation, but everything about this should be visible in developer console and debug info there – kaz Jan 04 '12 at 12:34
  • if I use console from Chrome and if I use 'jsonp' dataType I can see my data in it. but jquery can't process it right - because it's not a json data. so, it means that request can receive data in this case. but can't receive it, when using 'text' dataType. it's confusing me. – Dmitry Belaventsev Jan 04 '12 at 13:10