Unable to get my Json service


#1

Hello
Newbie in ionic.
unable to get my php service… here’s my test code
Thank’s for help

angular.module(‘starter.services’, [])

.factory(‘Chats’, function($http) {
// Might use a resource here that returns a JSON array

var url = "http://envieslocales.fr/testws5.php/?s=7";
var chats = $http.get(url);

return {
all: function() {
return chats;
},
remove: function(chat) {
chats.splice(chats.indexOf(chat), 1);
},
get: function(chatId) {
for (var i = 0; i < chats.length; i++) {
if (chats[i].id === parseInt(chatId)) {
return chats[i];
}
}
return null;
}
}
})


#2

use JSONP , other wise you need to open the Access-Control-Allow-Origin on your server


#3

thank’s a lot.
I ve searched a lot but don’t manage to use jsonp…
can you bring me example please?


#4

I’ve tried this

url = ‘http://envieslocales.fr/testws5.php/?s=7&callback=JSON_CALLBACK’;
$http.jsonp(url).success(function(response){
var chats = response;
});

and nothing works…


#5

what is the output of the console ?

use chrome console to get the output error and put it here, without more information no one can answer you


#6

php
return data as a JSON
exampe:

                 $result = mysql_query( "select * from chat");
                 $data = array();

		
		while ($row = mysql_fetch_assoc($result)) {
		   $data[] = $row;
		}
					
		echo json_encode($data);

                  // your service javascript
                 this.request("chat.php", params, function (res) {
                     var result = JSON.parse(JSON.stringify(res));
                 });

#7

Thank’s for help, I’ve found my pb…
the controller refresh the view BEFORE the service has done the $http…
that was my pb, I’ve solved with a resolve in my app.js