JSON Request using Javascript

Discussion in 'JavaScript' started by onewisp, Oct 1, 2007.

  1. #1
    Hi,

    I am having problems with trying to make a call another website which returns a json response using a JavaScript. I have been trying different techniques but all of them failed.

    This is the code that I have

    <script type="text/javascript">
    function loadJSON(url) {
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = url;
    headID.appendChild(newScript);
    }

    function processJSON(feed)
    {
    var jsonResponse = new Function("return " + feed)();
    document.writeln(jsonResponse);
    alert(jsonResponse.title);
    }

    I am not able to tell you the website since its confidential. Basically I am requesting by passing the web url such as this:

    <script type="text/javascript">
    loadJSON('http://www.somewebsite.com/code/load?id=654987');
    </script>

    if i type the url 'http://www.somewebsite.com/code/load?id=654987', it will return a JSON response of the form

    {"text":"textValue", "title":"titleValue"}

    I am trying to show the text and title in a <div> tag within the body. How can I make the request using Javascript?
     
    onewisp, Oct 1, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    As far as I know, you can add on your site the content obtained from other sites (like IFRAME do). For example: Google Adsense Ads works that way. But you can NOT read that content, because cross domain scripting is not available on browsers for security reasons.
     
    ajsa52, Oct 1, 2007 IP
  3. onewisp

    onewisp Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have tried using different techniques such as XMLHTTP, dojo.IFrame, and now I am trying to use RCP. Is there another technique that I missed?

    This whole project is for a sidebar Firefox extension which lets user login into the website and send a request to the www.something.com and get the json response and display it on the sidebar.

    Is this hard to do? Any examples that you can link me to?
     
    onewisp, Oct 1, 2007 IP