2

First: I am new to AJAX and just know some basics about PHP.

I want to do an AJAX post to a .php file. To do that I got this code:

var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();

$.ajax({
    url: 'http://api.geonames.org/findNearbyPostalCodes',
    type: 'GET',
    data: {lat: lat, lng: lng, radius: 20, maxRows: 100, country: 'CH', username: 'bbag8274'},
    success: function(scriptData, textStatus, jqXHR){
        $.ajax({
            url: 'privatkunden_data.php',
            type: 'POST',
            data: {xmldata: jqXHR.responseText, address: $('#address').val()},
            success: function(secondScriptData, textStatus, jqXHR){
                alert('suc');
            },

            error: function(jqXHR, textStatus, errorThrown){
                alert(jqXHR.responseText);
            }
        });                         
    },

    error: function(jqXHR, textStatus, errorThrown){
        alert('fail');
    }
});

If the first post is successful it executes the second one. But the second one executes the error: function. Here is my .php file:

$xmlroot = $_POST['xmldata'];

$doc = new DOMDocument;
$doc->loadXML($xmlroot);

foreach($doc->getElementsByTagName('postalcode') as $postalcode){
    $zipcodes = $postalcode->nodeValue . " ";
}

The problem is: The AJAX post returns an empty alert box (alert(jqXHR.responseText);) and the .php file returns an error:

 **PHP Warning:  DOMDocument::loadXML(): Empty string supplied as input**

So the error says my $xmlroot variable is empty, am I right? If yes, why is it empty? It should be filled in with informations from xmldata.

Suggestions are appreciated

Thanks in advance

5
  • 7
    You can't do ajax requests to domains that are not the current one unless you use jsonp. Check out Wikipedias article on Same-origin policy. Commented Jan 8, 2014 at 12:17
  • I don't know exactly what you mean. I can't do it in this way? Thanks for the link, I will check it Commented Jan 8, 2014 at 12:27
  • what is the lat and lng ? Commented Jan 8, 2014 at 13:05
  • I mean. Give us the real value of lat and lng. This way is gonna be easier for testing. Commented Jan 8, 2014 at 13:48
  • lat & lng are some coordinates. For example you can use lat=47.644200 and lng=9.180260 Commented Jan 8, 2014 at 14:19

2 Answers 2

1

The data in the first ajax call is coming right. Try to put the dataType as text and convert this text to xml on your PHP.

dataType: 'text'

Example:

$.ajax({
    url: 'http://api.geonames.org/findNearbyPostalCodes',
    type: 'get',
    dataType: 'text',
    data: {lat: '47.644200', lng: '9.180260', radius: 20, maxRows: 100, country: 'CH', username: 'bbag8274'},
    success: function(result){
      alert(result)
    }
});

Live example: http://jsfiddle.net/t5jA5/

Sign up to request clarification or add additional context in comments.

3 Comments

That didn't help :/ If I add dataType it doesn't load anymore
Mh okay. Then I've something other wrong in my code. I will look at it again and try to fix it. This answer is the correct answer to a code which works
After changing try to convert the XML on your PHP
1

Could it be that you specify POST parameters although you do a GET request?

change the URL in something like

http://api.geonames.org/findNearbyPostalCodes?lat=47&lng=9&username=demo

...and read : http://www.geonames.org/export/web-services.html

3 Comments

If I use GET I have all the parameters in the data:{} function and if I use POST I have all the parameters in the url: function. It shouldn't matter what I use. The only difference is that with GET the user will see the url and with POST not. Or am I totally wrong with this comment? :)
ok. sorry you're right . But something else is not right in the data line. Shouldn't it read data: '{"lat": 47.644200, "lng": 9.18026, "radius": 20, "maxRows": 100, "country": "CH", "username": "bbag8274"}',
Yeah that is what it reads. And this is successful but the second post isn't successful. Because in the second AJAX post it executes the error function and not the success function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.