Opened 7 years ago

Closed 7 years ago

#15273 closed defect (invalid)

dojo/request and handleAs: "xml" not working

Reported by: Kitson Kelly Owned by: Bryan Forbes
Priority: undecided Milestone: tbd
Component: Core Version: 1.7.2
Keywords: Cc:
Blocked By: Blocking:

Description

I am geting an empty object (e.g. {}) when doing the following with dojo/request:

I have the following file as helloworld.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<hello>world</hello>

And I do the following:

require(["dojo/request"], function(request){
  request("helloworld.xml", function(response){
    console.log(response.data);
  });
});

Change History (4)

comment:1 Changed 7 years ago by Bryan Forbes

Owner: set to Bryan Forbes
Status: newassigned

comment:2 Changed 7 years ago by Bryan Forbes

Resolution: invalid
Status: assignedclosed

This isn't a bug. A couple of comments on this snippet:

  • The second argument to request is an object, not a function. You should be chaining a then() call off the return.
  • You're not passing { handleAs: "xml" } to request(), so it handles the return as text.

comment:3 Changed 7 years ago by Kitson Kelly

Resolution: invalid
Status: closedreopened

Sorry, I was typing my example example wrong. I should have copied and pasted the code that I put in the documentation, instead of just typing stuff off the top of my head. Apologies...

This is what I had in the reference-guide as an example:

require(["dojo/request", "dojo/dom", "dojo/dom-construct", "dojo/json", "dojo/on", "dojo/domReady!"],
function(request, dom, domConst, JSON, on){
  on(dom.byId("startButton"), "click", function(){
    domConst.place("<p>Requesting...</p>", "output");
    request("./helloworld.xml",{
      handleAs: "xml"
    }).then(function(response){
      domConst.place("<p>response.data: <code>" + JSON.stringify(response.data) + "</code>", "output");
    });
  });
});
<h1>Output:</h1>
<div id="output"></div>
<button type="button" id="startButton">Start</button>

comment:4 Changed 7 years ago by Bryan Forbes

Resolution: invalid
Status: reopenedclosed

The problem is that is not valid XML so the browser fails to parse it. Change the XML to something like the following and response.data will be populated:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<h1>Output:</h1>
<div id="output"></div>
<button type="button" id="startButton">Start</button>
</root>
Note: See TracTickets for help on using tickets.