V
V
Valery2013-07-21 18:04:54
JavaScript
Valery, 2013-07-21 18:04:54

How to process JSON data which is actually XML?

I'm doing a cross-site request for geodata by IP. I suffered for a long time, no ways to resolve such a request with XML data worked. It only works if I do like this:

$.get('http://ipgeobase.ru:7020/geo?ip=89.107.39.118', function(data) {	
//тут как-то я получаю myregion из ответа 
}, 'jsonp');


Server response in XML format:
<?xml version="1.0" encoding="windows-1251"?>
<ip-answer>
<ip value="89.107.39.118"><inetnum>89.107.32.0 - 89.107.39.255</inetnum><country>RU</country><city>Ессентуки</city><region>Ставропольский край</region><district>Северо-Кавказский федеральный округ</district><lat>44.049809</lat><lng>42.857239</lng></ip>
</ip-answer>


To which the browser responds with “Uncaught SyntaxError: Unexpected token < ”, as I understand it, it is unable to process the angle bracket. I could extract the necessary data from this answer. But how to make the browser accept them without error? Maybe you can process the response before the built-in parsing?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
I
Ilya Sevostyanov, 2013-07-24
@ValeriyKr

I don't understand something or

 function  httpGet ( theUrl )
    {
    var  xmlHttp  =  null ;
 
    xmlHttp  =  new  XMLHttpRequest ( ) ;
    xmlHttp. open (  "GET" ,  theUrl ,  false  ) ;
    xmlHttp. send (  null  ) ;
    return  xmlHttp. responseText ;
    }

and parse responseText,
don't we?

K
kxyu, 2013-07-21
@kxyu

For jsonp to work, server-side support is needed - the returned result must be a valid javascript code, usually a function call with data as an argument, the function name is passed to the server as a get parameter. Whether the data will be json or xml is essentially not important - in this case, the service simply does not support this interaction option - choose another one, for example:
http://freegeoip.net/json/89.107.39.118?callback=callback

M
Maxim Timokhin, 2013-07-21
@timokhin

http://ipgeobase.ru:7020/geo?ip=89.107.39.118&json=1 - here is the output in json format

B
Boris, 2013-07-21
@ekho

there are three options:
1) make a “proxy” on the backend. Those. use ajax to pull your backend, which in turn will pull ipgeobase
2) bother with the old ways of "ajax" (iframe, etc.)
3) try to contact ipgeobase and ask them to return the correct Access-Control-Allow-Origin header

J
JustLuckyGuy, 2013-07-21
@JustLuckyGuy

replace 'jsonp' with 'xml'?
in general, see mana by $.get. I don't know what library you have.

E
ertaquo, 2013-07-21
@ertaquo

Why don't you specify XML format instead of JSONP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question