Posting an array using Titanium HttpClient - titanium-mobile

I am trying to post to a webservice using Titanium HttpClient like so:
var non_data = {
user_id: Facebook_ID,
"friends_ids[0]":friendIds[0],
"friends_ids[1]":friendIds[1]
};
var non_xhr = Ti.Network.createHTTPClient({
onload: function(){
Titanium.API.info('Status: ' + this.status);
Titanium.API.info('ResponseText: ' + this.responseText);
Titanium.API.info('connectionType: ' + this.connectionType);
Titanium.API.info('location: ' + this.location);
alert("Get_Non_Friends response: " +this.responseText);
}
});
non_xhr.open('POST', myURL);
non_xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
non_xhr.send(non_data);
But it doesn't seem to be getting the array elements right. Can anyone tell how to post and array of params.
Also I found a post on TIMOB that says to do something like this, which I am currently trying:
non_xhr.open('POST', myURL);
//non_xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
non_xhr.send('user_id=someData&friends_ids[0]=someData);
Can anyone tell me the best approach for this problem?

The problem seems to be with the send method. The send method should be something like this
non_xhr.send({paramName : non_data});
The paramName is the name which is required by the web service. Ex
non_xhr.send({
file: abc.jpg
});
Also its advised to have onerror method too just like onload method.

Related

Unable to get text from element in nightwatchjs

I'm currently using nightwatchjs (latest version) and I'm trying to get the text of the element below ("Welcome to the newest....."), but I can't seem to find it.
Currently, I'm using the following code;
browser.getText('css selector', '.bfs-details__about-seller__why-buy p', function(result) {
console.log('element text = ', result.value);
});
But I get an empty value.
Am I missing something obvious here?
thanks
As I see it, the problem is not the selector but how you try to log it.
Try console.log('element text = ' + result.value);
Or as an alternative (thats how I do it):
browser.getText('css selector', '.bfs-details__about-seller__why-buy p', function(result) {
myValue = result.value;
});
browser.perform(function () {
console.log(myValue)
});
Why use perform: Nightwatch API Perform

$http.get works, $http.post doesn't

So, I have this really simple snippet in a controller where I get data from an external file using $http.get, and it works great! But when I use $http.post, I get a "syntaxerror: unexpected token < at object.parse(native)" in my console.
I've included both versions below with the working $http.get commented out.
var blogCtrl = angular.module('blogCtrl', []);
blogCtrl.controller('articleCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
//$http.get('/angular_blog/assets/php/ajaxRequests.php?action=fetchSingleArticle&permalink='+$routeParams.articlePermaLink)
$http.post('/angular_blog/assets/php/ajaxRequests.php', {action: 'fetchSingleArticle', permalink: $routeParams.articlePermaLink})
.success(function(data) {
$scope.articleTitle = data.articleTitle;
$scope.articleAuthor = data.articleAuthor;
$scope.articleContent = data.articleContent;
$scope.publishDate = data.publishDate;
$scope.category = data.category;
$scope.categoryPermaLink = data.categoryPermaLink;
});
}]);
I already tried the suggestion in this question, but it gave the same result.
I figured it was better to make my PHP-file understand a json post request, so instead of trying to grab strings with $_POST['string'], do this:
$obj = json_decode(file_get_contents('php://input'));
and then when I want to use strings from the request:
$obj->{'string'}

Parse.com: Retrieving object by objectId does not work. Why? It is very simple query

I got an error with message "101 Object not found."
The below code is just copy from the official guide. And I changed the class name and the objectId.
I know this is very simple query but I don't know why? Help me how to debug in this case...
This code is in cloud code. I set up "applicationId" and "masterKey" in global.json.
Thanks..
require('cloud/app.js');
Parse.Cloud.define("sample", function(request, response) {
var GameScore = Parse.Object.extend("Item");
var query = new Parse.Query(GameScore);
query.get("XXXXXX", {
success: function(gameScore) {
},
error: function(object, error) {
console.error("error: " + error.code + " " + error.message);
}
});
});
I tend to use the promise method and I'd possibly rewrite it like this...
Parse.Cloud.define("sample", function(request, response) {
var query = new Parse.Query("Item");
// put this in as a debug message.
console.log("Just checking I'm here!");
query.get("XXXXXX").then (function(item) {
response.success(item);
}, function(error) {
console.error("error: " + error.code + " " + error.message);
response.error(error);
});
});
But it should work as it is. Odd. Are you sure the error message is coming from your code?
Try adding a log before it.
EDIT
It seems that permissions were not set properly on your item object.
With iOS you can specify a default ACL for objects at create time. You can also create a custom ACL object and pass it to the object when saving it.

dojo ajax request to spring mvc,getting http 400

before starting let me say that I am new to dojo and this is my first project in dojo:
when I am trying to send json data from rest client (some chrome ext) it working for me,I mean to say that my spring mvc part is working, but when i am trying to send the same json from dojo code I am getting http 400 exception
my dojo code:
postCreate : function() {
this.inherited(arguments);
var form = dom.byId("contactSubmit");
on(form, "click", function(evt) {
var box0 = registry.byId("inputEmail");
var box1 = registry.byId("inputName");
var box3 = registry.byId("message");
alert("values are: " + box0.get("value"));
jsonData = {"email":"some#gmail.com","inputName":"some name","message":"some msg"};
request.post("/pool/conta", {
data: jsonData,
handleAs: "json",
headers: {
"Content-Type": "application/json;charset=utf-8",
"Accept": "application/json"
}
}).then(function(text){
alert("values are send"+text);
});
});
}
the jason data that I am sending from rest client is which is working:
{"email":"some#gmail.com","inputName":"some name","message":"some msg"}
my spring mvc method is below:
#RequestMapping(value="/conta", method = RequestMethod.POST)
public #ResponseBody Contact getShopInJSON(#RequestBody Contact contact2) {
Contact contact = new Contact();
contact.setEmail("pro#gmail.com");
contact.setInputName("pro");
contact.setMessage("msg");
System.out.println("***********************"+contact2.getEmail());
return contact;
}
pool is name of application
The json data as passed in post request requires string to be crypted with "\" so that the javascript can handle the double codes as is within string(double quoted string).
Thus, the line
jsonData = {"email":"some#gmail.com","inputName":"some name","message":"some msg"};
would work if written as below
jsonData = " {\"email\":\"some#gmail.com\",\"inputName\":\"some name\",\"message\":\"some msg\"} " ;
Its working now, I have used toJson from dojo/_base/json" utility before passing it to request.post

Ajax prototype to load page then update hash

I have 3 page with different concept/layout/animation.
I'm using prototype & script.aculo.us
I have this in my navigation:
<ul>
<li>PAGE1</li>
<li>PAGE2</li>
</ul>
and this is in my js:
windows.location.hash: 'web';
function showPage() {
startloading();
var url: '/localhost/page2'+web;
new Ajax.Updater('maincontent', 'page2', { method: 'get' });
finishloading();
}
the question & problem is:
Why in windows location hash is still: /localhost/page1/#page2 with or without if I use var url?
All the animation in page 2 doesn't work, because I didn't put the header, but if put I it, I got double header and still the animation won't work either.
Can anybody give me the solution?
Thank you very much.
In your code
var url: '/localhost/page2'+web;
line throws error so hash cannot be changed. Fix it to
var url = '/localhost/page2'+web;
then it should work.
The correct way to update your hash is:
window.location.hash = '#'+yourValue;
Hard to tell what exactly you're trying to do with your function but there's a few things that are clearly a bit wrong.
function showPage(var) {
startloading();
var url: '/localhost/page'+var;
new Ajax.Updater('maincontent', url, { method: 'get' });
finishloading();
}
depending on what you're actually doing its fairly likely you would probably want something more like this:
function showPage(var) {
var url = '/localhost/page'+var;
new Ajax.Updater('maincontent', url, { method: 'get' ,
onCreate: function(){
startloading();
},
onComplete: function(){
finishloading();
}
});
}
Thats complete guesswork though, if you can provide more detail i can help more.

Resources