0

I'm making a widget on iphone but I can't get data from the url. On IE, I can get data. However, on chrome and on iphone I can't get the data but it only shows undefined instead of data.

function a() {
    var url="www.xxx.xxx";
    var request = new XMLHttpRequest();
    request.open('GET', url, false); 

    request.send();     
    xmlDoc = request.responseXML;
}

please help me!! I'm really appreciated for any answers.

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
  • 1
    [you should wait until an HTTP request is finished](http://www.mattlunn.me.uk/blog/2011/11/handling-an-ajax-response-in-javascript-with-or-without-jquery/) – Dmitry Pavliv Jan 01 '15 at 16:04
  • @simoco: His request is synchronous –  Jan 01 '15 at 16:19
  • 1
    @user: Does your server have the `Content-Type: text/xml` header set? –  Jan 01 '15 at 16:22
  • If you're unable to modify the server to set it, then see the [`overrideMimeType()`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#overrideMimeType()) function. –  Jan 01 '15 at 16:28
  • I added request.overrideMimeType("text/xml"); but it still doesn't work :( – user4410414 Jan 01 '15 at 16:42
  • Did you call it *before* the `.send()`? Are you unable to set the header? If so, try getting the `.responseText` instead of the `.responseXML` –  Jan 01 '15 at 19:19

3 Answers3

0

Create a function something like below,to receive the data from server.

request.onreadystatechange = function(){
        if(request.readyState == 4){            
            console.log(request.responseText);
        }
    }
dReAmEr
  • 6,986
  • 7
  • 36
  • 63
0

Please make sure that you are making request from the same Origin. That means if you are in site www.abc.com then you can make request for www.abc.com/download/ or www.abc.com/site and so on. But if you request for www.gdb.com then it will probably fail with this error in your console "No 'Access-Control-Allow-Origin' header is present on the requested resource." The browser prevents this activity for security reasons. It needs to be on the same domain.

Try using JQuery sometimes. It's API is very easy to use and is very helpful for doing tasks. You will need to add the script to the page first like this:

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>

You can download the script or use the live version and link to it like above.

Next you can make a call like this to make a GET request. Observer that it returns data when successful. This makes your job easy but remember you need to make call from same domain.

$.ajax({
    type: "GET",
    url: "http://wwww.something.com"
})
    .done(function( data ) {
        alert(data);
    })
    .fail( function(xhr, textStatus, errorThrown) {
        alert(xhr.responseText);
    });

To know more about Cross Site HTTP Requests: CORS Here is a thread that may help you to understand better: “No 'Access-Control-Allow-Origin'

Community
  • 1
  • 1
ZakiMak
  • 2,072
  • 2
  • 17
  • 26
  • It would be silly to load jQuery just do to a simple, easy XHR request. That's bad advice. And he said the request works in some browsers, so it's not likely a cross-domain issue. –  Jan 01 '15 at 19:21
  • That's just a suggestion. I assume the user may do more than just making simple calls in his application and JQuery might come in handy. – ZakiMak Jan 01 '15 at 19:28
-1

would setting the responseType work?
call the request.responseType = 'document'; before send.

ChinKang
  • 4,212
  • 2
  • 17
  • 29