0

I would like to be able to read information from a small page.

I have the address of a JSON service that displays the following information:

enter image description here

And I wish I could keep the number that appears.

I tested this example and work correctly, however when I try with my URL nothing happens. I do not know if I am to understand the problem correctly, but I wish someone could please help me.

If have any questions, I try to explain as best as possible.

I ask now apologize for the inconvenience.

The code that I used

var getJSON = function(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        resolve(xhr.response);
      } else {
        reject(status);
      }
    };
    xhr.send();
  });
};

getJSON('http://MYADDRESS/json.do?_ULN[1]').then(function(data) {
    alert('Your Json result is:  ' + data.result); //you can comment this, i used it to debug

    result.innerText = data.result; //display the result in an HTML element
}, function(status) { //error detection....
  alert('Something went wrong.');
});
6
  • Any errors in the console? Commented Apr 1, 2015 at 10:45
  • Suppose your request doesn't work due to cross domain limitation Commented Apr 1, 2015 at 10:47
  • Thanks for reply. Just nothing appears . If I try with https gives me error message. But if I try only with http doesn't give me nothing . Commented Apr 1, 2015 at 10:55
  • @phts There any solution ? Commented Apr 1, 2015 at 10:56
  • Read wiki's link in @user2478905 answer and read about possible solutions how to avoid this limitations there. Also there are many questions/answers about that on SO. Commented Apr 1, 2015 at 10:58

2 Answers 2

1

You can't for security reasons. See the same origin policy for JavaScript.

There are some workarounds that exploit browser bugs or corner cases, but using them is not recommended.

The best approach is having a server-side proxy that receives Ajax requests, and in turn, sends HTTP requests to other servers. This should be carefully implemented by sanitizing input and whitelisting the types of requests that are sent, and the servers that are contacted.

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

1 Comment

Thanks for reply Sir. Basically what you're trying to say is that I will not be able to read this information , right?
1

Your problem exist because of the browser Same-origin policy.

One solution to your problem is to use the method JSON-P or CORS. The method is well explained here : http://json-p.org/ and here : http://www.sitepoint.com/jsonp-examples/.

2 Comments

Thank you for reply. You have right, I need study JSON-P metlhod. Is it possible to give me some practical example please?
There is a simple example : http://codepen.io/CWSpear/pen/srulx

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.