Docunext


jQuery Post XML Response Parsing

March 6th, 2008

Finally figured this out... something obvious as usual. I had to set the content type to text/xml for that the post request was receiving. Duh, after that, it was easy. I also read somewhere that jQuery dropped support for xpath, I'm fine with that, the DOM seems to fit better with client side work.

I'm sticking with XML because I'm such a huge fan of it. JSON looks cool, but I'm happy with XML. Anyway, here's the code I got working:

$.post("http://www.example.com/blah",
{
  'barfy': 'blah'
},
function (data){
  alert( ($("menu",data).attr("id")));
});

Then the magnificient XML document which is sent to the jQuery ajax client:

<menu id="hi">    blah</menu>

This will result in "hi" getting alerted. For the "blah" value, you'd use ($("menu",data).text()) as the DOM locator. The "data" text is in the jQuery ()'s because it is "context" of the reference, otherwise the query would be looking for menu in the existing page.

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022