Having trouble parsing an XML received from AJAX request

I am encountering an issue with the code below:

$.ajax({
    type: "POST",
    dataType: "xml", 
    url: getUrl('/GetPeriodicStats/'), 
    data: XML.innerHTML,//some xml,
    success: function(c)
    {

After receiving the XML data in the client-side, I attempt to parse it to extract specific information. The XML structure is as follows:

<command name=GetApLevelNumUlBytesSum all=1 >650</command>

I then try to retrieve the value 650 and display it in an alert using either of the following methods:

$(c).find('command').each(function(){
                var val = $(this).text();
                alert(val);
                });
var val = $(c).text();
alert(val);

However, I do not receive any alerts at all. Could you please help me identify what went wrong?

Answer №1

Consider modifying your code with the following changes.

$(c).find('command').each(function( index, value ){
  var val = $(value).text();
  alert(val);
});

Updated: in order to retrieve the accurate value.

Answer №2

Consider employing the jQuery method parseXML() for parsing XML data.

xmlData = $.parseXML( xmlInput ),
$xml = $( xmlData ),
$commands = $xml.find( "command" );
alert($commands.text());

Answer №3

It appears that your XML code is incorrect. While HTML may allow attribute values without quotes, in XML this is not permitted.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can someone assist me in assigning a cookie value using the $.post() method?

Currently, I am running a script that utilizes jQuery's $.post to set a $_COOKIE['menu_item_id'] as the last insert id. Despite successfully setting the cookie value, I am facing an issue where I can't access this cookie in other parts ...

Is there a way to dynamically pass values to a form in React?

Learning React on my own has been challenging, especially when trying to accomplish what I thought would be a simple task. To put it briefly, I have a menu with several items. I aim to select a menu item and have a form open next to it. The form shoul ...

Load Joomla module upon document completion

After creating a module, I noticed that it is causing the page to load slowly. I tried looking up how to load modules using ajax, but couldn't find any helpful information (possibly due to language barriers as English is not my native language). My qu ...

Display a div element with Angular's ng-show directive

I am encountering difficulties with implementing ng-show and $pristine. Below is the code snippet (also available on CodePen): <blockquote ng-show="!comment.author.$pristine && !comment.rating.$pristine && !comment.comment.$pristine"&g ...

Convert TypeScript-specific statements into standard JavaScript code

For my nextjs frontend, I want to integrate authentication using a keycloak server. I came across this helpful example on how to implement it. The only issue is that the example is in typescript and I need to adapt it for my javascript application. Being u ...

Provide input data to the function for delivery purposes

Creating a simple webpage with a form input that triggers a script to request JSON data from the server based on the user's input. The challenge is passing the value from the input field to a function for processing. var search = document.querySele ...

Button to scroll down

I have successfully implemented a #scrolldownbutton that scrolls to the first component. However, I am now attempting to modify it so that when the button is clicked, the page smoothly scrolls within the viewport and stops at the partially visible componen ...

Currently, I am working on a project and encountering an issue with html2canvas

const handleDownloadImage = async () => { try { const canvas = await html2canvas(cardRef.current); console.log(cardRef.current); const imageData = canvas.toDataURL("image/png"); setImageUrl(imageData); } catch ( ...

JavaScript JCrop feature that allows users to resize images without cropping

I'm currently attempting to utilize JCrop for image cropping, but I'm running into frustratingly incorrect results without understanding why. The process involves an image uploader where selecting an image triggers a JavaScript function that upda ...

Ways to adjust timestamps (DayJs) by increments of 1 minute, 5 minutes, 15 minutes, 30 minutes, and more

Currently, I am exploring time functionality within React Native by utilizing DayJs. I have noticed a slight inconsistency when comparing 2 different points in time to calculate the time difference. Typically, everything works smoothly such as with 10:00 ...

"Surprising outcomes when using the splice method on an array

Exploring the functionalities of Array.splice(). deleteCount: A whole number indicating the quantity of old array elements to eliminate. Understood. It seems clear. I aim to extract the final 4 items in the array. I trust that refers to elements? arr. ...

Storing the created .wav file on the server using PHP

Is there another way to handle this? How can I manage streaming of a WAV file? I'm currently working on a platform that allows users to create their music compositions and the system outputs a .wav file for each creation. While I can play the mus ...

Having issues with AJAX and .change() function not functioning correctly?

I am working on two drop-down menus. The first menu displays the provinces in the country, and when a province is selected, the second menu should show the districts within that province. The following code is for displaying the provinces: $(document).re ...

Show items in the sequence of clicking

Is there a way to display elements in the order they're clicked, rather than their order in the HTML using jQuery? For example: CSS code: .sq{ display:none; } HTML Code: <a href="#" id="1">A</a> <a href="#" id="2">B</a> ...

Can someone guide me on incorporating bluebird promises with request-extensible?

I'm interested in utilizing the bluebird library to create a promise-based asynchronous web client. Currently, I have been using the request-promise package for this purpose. To get started, I simply include the following lines of code at the beginnin ...

Retrieve the ID of the nearest div without it being executed twice

I am currently working on implementing a close button using 'onclick' functionality. The goal is to hide the parent ID element when the user clicks the button, as shown below. function displayNone() { var id= $('.btnClose').closest ...

I need guidance on how to successfully upload an image to Firebase storage with the Firebase Admin SDK

While working with Next.js, I encountered an issue when trying to upload an image to Firebase storage. Despite my efforts, I encountered just one error along the way. Initialization of Firebase Admin SDK // firebase.js import * as admin from "firebas ...

Is there a way to automatically close the Foundation topbar menu when a link is selected?

On my single page website, I am utilizing Zurb Foundation's fixed topbar which includes anchor links to different sections of the page. My goal is to have the mobile menu close automatically whenever a link inside it is clicked. As it stands now, whe ...

Ajax call for updating PDO fails despite correctly passing POST variables

When I submit the form and update the database individually, everything works as expected. However, when I use AJAX to submit the form, the correct POST variables are passed but I encounter a PDO exception that I am unsure how to resolve because it functio ...

The process of transferring information from JSON to Java

I need help converting the start date from a JSON object to Java in my JSP page. The AJAX call is returning the date as 153452636268, but I want it in a different format. Can someone please provide assistance? $.ajax({ type: "GET", url: ...