Rest API and XmlHttpRequest status

Author Message

Emmanuel Bichet

Thursday 23 June 2011 3:07:57 am

Hello,

I'm trying to use the REST api in order to retreive some content with ajax.

When I try the url http://my_host/api/ezp/v1/content/node/2 on my browser (chorme or firefox), I get the informations but not with a xmlHttpRequest.

Here is my HTML code :

 

 <html>
<head>
<script language="JavaScript">

 function submitForm()
 {
    var xhr=null;

   try
   {
     xhr = new XMLHttpRequest(); 
   } catch(e)
   
  }

  xhr.onreadystatechange = function()
   { 
     dyn = document.getElementById('dyn');
     dyn.innerHTML="Wait server...";                
     if(xhr.readyState == 4)
     {
        if(xhr.status == 200)
        { 
             dyn.innerHTML="Received:" + xhr.responseText; 
        } 
       else 
        { 
              dyn.innerHTML="Error: returned status code " + 
              xhr.status + " " + xhr.statusText+" Response : "+ xhr.responseXml; 
       } 
    } 
 }; 

 xhr.open("GET", "http://<a href="http://my_host/api/ezp/v1/content/node/2">my_host/api/ezp/v1/content/node/2</a>", true);                
 xhr.send(null); 
}

</script>         
</head>
               
<body>
    <input type="submit" value="Submit" onclick="submitForm()" />
    <textarea type="text" id="dyn" value=""></textarea>  
</body>

</html>

The status of the xhr is always 0 and the responsetext is empty.

Any clue ?

André R.

Thursday 23 June 2011 4:05:27 am

Try using jQuery/YUI perhaps? Or at least debug it using Firebug/Developer tools -> Network.
I'm assuming url in code example got modified by this forum as having a tag inside url would not work. 

Some nitpicks:

- Your writing to the DOM on every readystate callback, probably causing rendering reflows on every call.
- 'dyn' is now a global variable
- This will not work in IE 6, it does not have XMLHttpRequest object but an ActiveX object instead
- I think you need to define xhr.onreadystatechange after xhr.open, but most people have have moved on to use libs for these things by now so not sure anymore. 

 

If you absolutely have to code against pure javascript, try taking stuff from here maybe:
https://github.com/ezsystems/ezoe/blob/master/design/standard/javascript/ezoe/ez_core.js#L774

Good luck ;)

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Emmanuel Bichet

Thursday 23 June 2011 5:21:08 am

Thank for your answer André, but my code was just an example. I've also tried Jquery and the result is the same :

- the readystate is 4 (whitch mean the REST api is sending something)
- the status of the response is 0 (200 is expected normally)
- the responseText is empty (the json format of content/node/2 is expected)

I've check the php code in kernel/private/rest. The code make an intensive use of eZ/zeta components MvcTools. The response is created by the MvcResult class. When this class is instanciated, the status properties is set to 0. I think this is the problem. May be there is some change to do in the rest.ini settings...

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.