Forums / Developer / Rest API and XmlHttpRequest status

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...

eZ debug

Timing: Jan 29 2025 13:47:04
Script start
Timing: Jan 29 2025 13:47:04
Module start 'content'
Timing: Jan 29 2025 13:47:04
Module end 'content'
Timing: Jan 29 2025 13:47:04
Script end

Main resources:

Total runtime0.0168 sec
Peak memory usage2,048.0000 KB
Database Queries4

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0052 587.9531180.8359
Module start 'content' 0.00520.0055 768.789198.1172
Module end 'content' 0.01080.0060 866.906374.6875
Script end 0.0168  941.5938 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002112.2418120.0002
Check MTime0.00095.6306120.0001
Mysql Total
Database connection0.00053.012310.0005
Mysqli_queries0.002112.420340.0005
Looping result0.00000.083620.0000
Template Total0.005733.610.0057
Template load0.00085.021410.0008
Template processing0.004828.572610.0048
Override
Cache load0.00063.304110.0006
General
dbfile0.00158.9787100.0002
String conversion0.00000.029830.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs