www.webdeveloper.com
Results 1 to 2 of 2

Thread: JavaScript XML parse and proxy

  1. #1
    Join Date
    Jan 2007
    Posts
    32

    JavaScript XML parse and proxy

    Hi guys,

    I have a problem on parsing an XML using JavaScript.

    This is my code


    Code:
    function loadXMLDoc(dname) 
    {
    var xmlDoc;
    if (window.XMLHttpRequest)
      {
      xmlDoc=new window.XMLHttpRequest();
      xmlDoc.open("GET",dname,false);
      xmlDoc.send("");
      return xmlDoc.responseXML;
      }
    // IE 5 and IE 6
    else if (ActiveXObject("Microsoft.XMLDOM"))
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async=false;
      xmlDoc.load(dname);
      return xmlDoc;
      }
    alert("Error loading document");
    return null;
    }


    then I load the XML:
    Code:
    xmlDoc=loadXMLDoc("proxy.php?url=http://www.google.com/ig/api?weather=paris,france&hl=sv&u=c");
    my proxy.php looks like:

    PHP Code:
    <?php
    $ch 
    curl_init();
    $timeout 30;
    $userAgent $_SERVER['HTTP_USER_AGENT'];
     
    $url $_REQUEST['url'];
    $hl $_REQUEST['hl'];
     
    $fullGoogleWeatherPath $url "&hl=" $hl;
     
    curl_setopt($chCURLOPT_URL,$fullGoogleWeatherPath);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
    curl_setopt($chCURLOPT_USERAGENT$userAgent);
     
    $response curl_exec($ch);     
     
    if (
    curl_errno($ch)) {
        echo 
    curl_error($ch);
    } else {
        
    curl_close($ch);
        echo 
    $response;
    }
    ?>

    my server has CURL and PHP installed and the proxy page seems to work just fine when I render it in the browser, but when I try to parse using JavaScript I don't get the data.
    I tried to save locally the XML file without using the proxy and it works, so it is probably related to how the Javascript is loading the proxy XML.

    Any clue where I can focus, I have been spending hours around it without any success.

    thanks for your help.

    Max.

  2. #2
    Join Date
    Jul 2009
    Posts
    45
    Just use jQuery. It's a LOT easier.

    var url = "http://www.google.com/ig/api?weather=paris,france&hl=sv&u=c";
    $.get( url, function(data){
    alert( data );
    });

    http://net.tutsplus.com/tutorials/ja...m-an-xml-file/
    Or google xml jquery.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles