Parse xml From WebService To Json (Important)

This is very important: Funny story, I was developing an app and I needed to pass data from a website's  Web Service to an app. This is the only way to get access to the database data from an Ionic Cordova app because Javascript does not have inbuilt functions to connect to the database. In this case, you will have to build a Web Service, sort of a middle layer that communicates with the database. Most of the Web Services return data as XML instead of JSON which is really lightweight and easy to get access to the objects. The problem is that sometimes in JavaScript/TypeScript it is really hard to parse XML to JSON, there is an error that gets thrown when trying to pass XML to JSON. The solution is to convert XML or request data to be in form of TEXT in Http Headers you pass to the Web Service. 

When you define or specify the type of Data you want the Web Service to return in an AJAX API call, then Stringify that data returned, then parse it into JSON. Funny fact: Make sure when you replace , make sure you leave out '[' and ']'. This is the only way JSON will know that is an object. It took me 2 days or even longer, because the first time I solved this problem, I did not document it.  The code is shown below: It's a messy code but you get the idea.

//This function will call web api shungarts

url: "",
type: 'GET',
data: {_apiKeypi:'PutAPI_KeyHere'},

dataType: 'Text',
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//var mt = data.toString()
var cleanData = data.toString().replace('','')
var Stripped = cleanData.toString().replace('','')
//var j = JSON.stringify(Stripped)
// console.log(j)
var LastStripp = Stripped.replace('','')
var StringF = JSON.stringify(LastStripp)
var J = JSON.parse(StringF)
var k = $.parseJSON(J)

//var m = JSON.stringify(data)
// var me = $.parseJSON(m)
// var mk = JSON.parse(JSON.stringify(data));
//var k = JSON.parse(mk)
//var yourval = jQuery.parseJSON(JSON.stringify(data));
// console.log('_______________')
// console.log(m)

error: function (xhr, textStatus, errorThrown) {

console.log('Error in Operation:=> ' + textStatus +': '+ errorThrown);




© 2024 - ErnesTech - Privacy
E-Commerce Return Policy