0

I have the following XML which is returned to me after making an AJAX request.

<entry xml:base="url" xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>URL</id>
<category term="SomeCategory" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"></category>
<link rel="edit" title="Table" href="Table(56)">
<link rel="SomeURL" type="application/atom+xml;type=entry" title="table" href="table(56)/table1">
<link rel="SomeURL" type="application/atom+xml;type=entry" title="table" href="table(56)/table2">
<link rel="SomeURL" type="application/atom+xml;type=feed" title="table" href="table(56)/table3">
<link rel="SomeURL" type="application/atom+xml;type=entry" title="table" href="table(56)/table4">
<title></title>
<updated>2016-01-16T02:35:51Z</updated>
<author>
    <name></name>
</author>
<content type="application/xml">
    <m:properties>
        <d:NewlyCreatedId m:type="Edm.Int32">56</d:NewlyCreatedId>
    </m:properties>
</content>

I'm trying to get the value 56 out of it using jQuery, this is my success method inside my AJAX method:

success: function (data) {
    var selectedId = data.find('d:NewlyCreatedId').val();
    alert(selectedId);
}

But this produces the error:

Uncaught TypeError: data.find is not a function

Can someone suggest how I go about pulling the value our of this XML document please.

* Update *

I've tried that was suggested in the answers, however this is now returning null where I alert the selectedId.

Inside my success method I now have this:

var selectedId = $(data).find('d\\:NewlyCreatedId').text();

Updated the question with the complete XML document

* update *

I've been battling this all day, I changed my datatype within my ajax method to xml, tried looping through but with no avail. Can someone shed some light on this?

2 Answers 2

1

You need to turn data in to a jQuery object to be able to call find(). Also note that you will need to escape the : in the tag name, and you need to use text() to get the 1 value from the node, not val(). Try this:

success: function (data) {
    var selectedId = $(data).find('d\\:NewlyCreatedId').text();
    alert(selectedId);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for providing the code snippet, when I try your code and alert selectedId it appears to empty, even thought I can view the response in the browser and it has a valid number
1

Or you could also pass the dataType option to the $.ajax request:

$.ajax({
  url: '/x.xml',
  dataType: 'xml',
  success: function (xmlData) { console.log(xmlData); }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.