JSON Parse error: Unrecognized token '<' in titanium - ios

I'm receiving the error JSON Parse error: Unrecognized token '<'. but only on IOS. in android it is working fine and the JSON seams to be right. (You can put the link in your web browser and se). The error is in this line Data = JSON.parse(this.responseText); but i can't understand why. and why does is work on android and not in IOS?
var client = Ti.Network.createHTTPClient({
onload : function(e) {
Data = JSON.parse(this.responseText);
Size = Object.keys(Data).length;
AddList();
},
onerror : function(e) {
},
timeout : 15000
});
client.open("GET", http://lamadeus.virtualweb.pt/site/app_mobile/teste.php?act=getprodsdestaque);
client.send();

Have you tried printing the responseText on iOS? Usually this error indicates that the request is receiving a HTML instead of JSON.

Check after replacing :
< with <
> with >
& with & in your this.responseText and after that try to parse.

Related

Titanium Appcelerator API call error - HTTP ERROR

I am having a website which contains login page. When user tries to log in using username and password. Data is being passed in Form Data. Please have a look as following image to get idea.
Now I want to use the same api in my Titanium application and get all details or logged in user which i am performing using below mentioned code.
var url= "http://www.randomwebsite.com/login/";
var jsonData = {
username: "admin",
password: "password1"
};
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(e) {
var obj = JSON.parse(this.responseText);
alert("DATA IS " + JSON.stringify(obj));
};
xhr.onerror = function(e) {
Ti.API.info("ERROR " + e.error);
};
xhr.onsendstream = function(e){
Ti.API.info("onsendstream");
};
xhr.ondatastream = function(e){
Ti.API.info("ondatastream");
};
xhr.open('POST',url);
xhr.send(JSON.stringify(jsonData));
I am getting HTTP error. I even tried setting xhr.setHeader('Content-Type','application/json') as well as verified url its same as that is being used by website. Can any one help me out with this ? Or is there any way in order to make sure that titanium code passes data in form-data ? Or any suggestion regarding this would be of great help.
Its working fine now. Mistake that I was doing is that i was stringifying text when data was being send. So changing xhr.send(JSON.stringify(jsonData)) to xhr.send(jsonData) works for me. Hope so this would help some one.

Titanium isn't posting values to ColdFusion

This is my Titanium code:
var loginReq = Titanium.Network.createHTTPClient({
onload: function(e){
// just displays the response
var webview = Titanium.UI.createWebView({html:this.responseText});
win.add(webview);
}
});
loginReq.open("POST",url);
var params = {
email: email.value,
passowrd: password.value
};
loginReq.send(params); // this is sending nothing according to a CF variable dump
The ColdFusion page just dumps all the variables, and it shows up on the iPhone emulator. But it's giving me an empty struct for the variables, which means no variables are actually getting sent in.
How do I fix my Titanium code to actually post data?
If you want to send post data to a script you will have to to set the header accordingly:
loginReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
If you are sending JSON data to an API, you might need to stringify your parameters to send them:
loginReq.send(JSON.stringify(params));
Edit:
According to the docs this is done automatically (see comments). In some of my network clients I had to do that explicitly, though...
Moreover, you might also want to implement the onerror callback, so in case your call fails for any reason you will know why:
var loginReq = Titanium.Network.createHTTPClient({
onload: function(e){
// just displays the response
var webview = Titanium.UI.createWebView({html:this.responseText});
win.add(webview);
},
onerror: function(e) {
Ti.API.debug('Status: ' + this.status);
Ti.API.debug('Response: ' + this.responseText);
}
});

Esri API 10.2 - Error code: 400 "Unable to complete operation"

I am developing an iOS Application for collecting data following "FeatureLayerEditingSample"(you can find it here: FeatureLayerEditingSample).
I use Esri's iOS sdk version 10.2
This error occurs random sampling when I try to add a new feature (simple or with attachments) to my feature layer.
Moreover sometimes only one or two specific attachments cannot be sync and the error says "because unable to complete operation".
Any ideas?
When using REST API to add new feature, please make sure you do your request using POST and make sure you have included f and features parameters in the POST body.
Below is an example of the POST request in JavaScript(AngularJS) - it can be easily transferred to any other language:
$scope.addFeature= function(){
var url = "http://services6.arcgis.com/dD0xfCNJ6qLYAvCQ/arcgis/rest/services/US_Election_2016/FeatureServer/0/addFeatures";
var newFeature = {
"geometry" : {"x" : -122.504002, "y" : 45.448060},
"attributes" : {
"CandidateName" : "Hillary Clinton"
},
"spatialReference" : {
"wkid" : "4326"
}
};
var features = [];
features.push(newFeature);
var featuresString = JSON.stringify(features);
data = "f=json&features="+featuresString;
var config={
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}};
$http.post(url, data, config)
.then(
function(response){
console.log(response);
},
function(response){
console.log(response);
}
);
}
The problem was I was using a versioned database. Removing the version all worked again.

Highcharts-Export Server for Java JSON parsing issue

I configured the highchart export server for Java from the current master in GitHub. When I tried to export an image using the demo page a corrupted file was returned. I have debugged to code and found out the following message is returned as the result in validate method in ServerObjectFactory.java
String result = server.request("{\"status\":\"isok\"}");
The value of the result String is "Failed rendering:SyntaxError: Unable to parse JSON string"
What can be the cause for this issue.
Thank You
That's very strange. The syntax of the JSON string is syntactically correct. I can point out where from the error is generated.
In the highcharts-convert.js file in highcharts-ecport-convert/src/main/resources/phantomjs the incoming request is expected to be a JSON string and is parsed, see Line 469
function (request, response) {
var jsonStr = request.post,
params,
msg;
try {
params = JSON.parse(jsonStr); // L469
if (params.status) {
// for server health validation
response.statusCode = 200;
response.write('OK');
response.close();
} else {
....
Set in a console.log(jsonStr) before JSON.parse(jsonStr) . I hope this give you a clue of why it's throwing a error.

Working with XML in a Firefox Add-on(ex Jetpack)

I'm currently developing a Firefox add-on(using https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/ ) that consumes an API where the return data is in xml.
My problem is that I need to parse the returned data, and would like to do that using a xml object.
Since the request module only supports JSON and Text ( https://addons.mozilla.org/en-US/developers/docs/sdk/1.0/packages/addon-kit/docs/request.html#Response ) I need to convert the response.text to XML.
The code looks like this:
var Request = require('request').Request
.......
var req = Request({
url: https://to-the-api.com,
content: {
op: 'get-the-data-op',
password: "super-sec",
user: "username"
},
onComplete: function (response) {
dataAsText = response.text;
console.log("output: " + dataAsText);
}
});
req.post();
I have tried to user (new DOMParser).parseFromString(response.text, 'text/xml') but unfortunately it just fails with a error like ReferenceError: DOMParser is not defined
The question is if anyone of you guys have been able to create a Xml object inside a Firefox add-on, and if so, how?
Looks like the capability to parse response as xml was present, but has been removed. check out this bugzilla reference
Can't you use a normal XMLHttpRequest if you want to process the response as XML?
If DOMParser is unavailable you can try E4X:
var xml = new XML(response.text);
alert(xml.children().length());
You want to use the XMLHttpRequest object to handle your xhr request. Then when you get a response back access the responseXML object of the request variable. In the responseXML you'll have the documentElement and can use the querySelectorAll or querySelector to find elements you want. In each element you want just grab the textContent you need.
Here's an example to get you going (this looks for the 'xmls' element in the response):
var request = new require("xhr").XMLHttpRequest();
request.open('GET', 'https://to-the-api.com', true);
request.onreadystatechange = function (aEvt) {
if (request.readyState == 4) {
if(request.status == 200) {
var xmls = request.responseXML.documentElement.querySelectorAll("xmls");
for (var i = 0; i < xmls.length; i++) {
console.log("xml", i, xmls[i], xmls[i].textContent);
}
}
else {
console.log('Error', request.responseText);
}
}
};
request.send(null);

Resources