0

I'm trying to get a time data from json with the code bellow, but i get a error instead of the data, what can be happening?

$.ajax({
    type: "POST",
    url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
    success: function(data) {
        alert()
        data.location;
        $("div").html(data);
    },
    error: function(data) {
        $("div").html('can not get the json');
    }
});

Here is a fiddle: http://jsfiddle.net/jodgjqwf/

Caio Kawasaki
  • 2,894
  • 6
  • 33
  • 69
  • 4
    Open your console! `No 'Access-Control-Allow-Origin' header is present on the requested resource.` – tymeJV Sep 25 '14 at 19:49
  • its due to cross domain request – abdullacm Sep 25 '14 at 19:50
  • 1
    "i get a error" — It generally helps if you (a) Share the error with us when asking for help and (b) Google it first – Quentin Sep 25 '14 at 19:50
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – DeadCalimero Sep 25 '14 at 19:51
  • http://stackoverflow.com/questions/21375467/cross-domain-ajax-request-error-solved – abdullacm Sep 25 '14 at 19:54
  • @Quentin You are incorrect. The jsfiddle is breaking the cross origin rule because it is requesting from jsfiddle. The problem is that he is POSTing to the url and not GETting the URL. This is not a duplicate. – teewuane Sep 25 '14 at 19:54
  • 1
    @teewuane, the problem is still the same you still cannot bypass the cross origin rule with a GET instead of a POST, the server needs to implement CORS in order for that to happen – Patrick Evans Sep 25 '14 at 19:56
  • Your code is good. The problem is related to the web service. Try to replace your URL line with this one `url: "http://rest-service.guides.spring.io/greeting",` to see that everything is ok. – ROMANIA_engineer Sep 25 '14 at 19:59

1 Answers1

-1

Is http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro expecting you to POST to it? I just hit the url and it returned the JSON to me.

Change "POST" to "GET".

$.ajax({
    type: "GET",
    url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
    success: function(data) {
        $("div").html(data);
    },
    error: function(data) {
        $("div").html('can not get the json');
    }
});
teewuane
  • 5,524
  • 5
  • 37
  • 43
  • 1
    even with GET or POST unless the user is on the same domain, or the server uses CORS the request will fail due to cross domain limitations – Patrick Evans Sep 25 '14 at 19:54
  • @PatrickEvans That's true. I believe they are probably making the request from their own domain. I could be wrong, but I'm sure they aren't using jsfiddle for production ;) – teewuane Sep 25 '14 at 19:58
  • 1
    http://jsfiddle.net/jodgjqwf/1/ - That change does not fix it. – Quentin Sep 25 '14 at 19:58
  • `curl 'http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro' -X POST` gives the data. The problem is definitely not because it is POST. – Quentin Sep 25 '14 at 19:59
  • @Quentin You are right, it doesn't fix the jsfiddle. But it might fix the code they are running in their actual environment. – teewuane Sep 25 '14 at 20:01
  • I doubt the OP's domain is `www.previsaodotempo.org` though – Patrick Evans Sep 25 '14 at 20:01
  • How could it possibly fix the code they are running in their actual environment? I just proved that changing from POST to GET makes no difference to the response the service gives! – Quentin Sep 25 '14 at 20:01
  • @Quentin, you are probably right. And to answer your question, "How could it possibly fix the code they are running in their actual environment?"... They possibly could be using a domain that is allowed in the CORS rules for the service at previsaodotempo.org. And there could possibly be a rule in place preventing POSTs but allowing GETs. Especially if the service is not owned by the original poster. In that case it would make sense to allow GETs and not POSTs. So, now that I'm done playing "House", it would be nice for the original poster to give more info though I'm guessing they won't. – teewuane Sep 25 '14 at 20:37