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.

eZ debug

Timing: Jan 18 2025 02:43:18
Script start
Timing: Jan 18 2025 02:43:18
Module start 'layout'
Timing: Jan 18 2025 02:43:18
Module start 'content'
Timing: Jan 18 2025 02:43:18
Module end 'content'
Timing: Jan 18 2025 02:43:18
Script end

Main resources:

Total runtime0.6871 sec
Peak memory usage4,096.0000 KB
Database Queries59

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 589.0547152.6250
Module start 'layout' 0.00600.0029 741.679739.4688
Module start 'content' 0.00890.6767 781.1484550.5156
Module end 'content' 0.68560.0014 1,331.664112.1563
Script end 0.6871  1,343.8203 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.4842160.0002
Check MTime0.00140.2004160.0001
Mysql Total
Database connection0.00090.135610.0009
Mysqli_queries0.620590.3132590.0105
Looping result0.00050.0769570.0000
Template Total0.659195.920.3295
Template load0.00210.305320.0010
Template processing0.657095.618320.3285
Template load and register function0.00020.023510.0002
states
state_id_array0.00070.104910.0007
state_identifier_array0.00110.152920.0005
Override
Cache load0.00170.2517320.0001
Sytem overhead
Fetch class attribute can translate value0.00050.070120.0002
Fetch class attribute name0.00120.178440.0003
XML
Image XML parsing0.00080.119920.0004
class_abstraction
Instantiating content class attribute0.00000.001640.0000
General
dbfile0.00080.1187170.0000
String conversion0.00000.001440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
8content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 21
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs