Need Help ! Post data to RESTful service


#1

Hello
I am fairly new to web service. I made a RESTful web service and integrated it to my web server.
Here is my web service API

public function service_get()
{
      $data['services'] = $this->m_klien->getServiceList(); 
	  $this->response($data['services']);
}
public function vehicle_get()
{
    $data['get_vec'] = $this->m_vehicle->get_all();
	$this->response($data['get_vec']);
}

public function order_post()
{
	$data = array
	(
        'order_date' => $this->post('order_date'),
        'start' => $this->post('start'),
        'end' => $this->post('end'),
        'plate_num' => $this->post('plate_num'),
        'vehicle_type' => $this->post('vehicle_type'),
        'order_type' => $this->post('order_type'),
        'id_clients' => $this->post('id_clients'),
        'id_mechanic' => $this->post('id_mechanic'),
        'payment_deadline' => $this->post('payment_deadline'),
		'payment_amount' => $this->post('payment_amount'),
		'status' => $this->post('status')
    );
    $this->m_klien->setOrder($data, null);
}

Now i want to use the post method to save data to my server. Here is what i’ve been doing :
I followed the instruction here http://learn.ionicframework.com/formulas/backend-data/
my service :

.factory('Post', function($resource) {
         return $resource('http://192.168.137.1/MYWEB/api/Myserver/order');
})

on my controller :

$scope.dummy = {
	    order_date :'2015-09-25 09:00:00',
            start :'2015-09-27 09:00:00',
            end :'2015-09-27 09:30:00',
            plate_num :'AB1234XYZ',
            vehicle_type : '7',
            order_type : 'mobile',
            id_clients : '1',
            id_mechanic : '1',
            payment_deadline : '2015-09-25 13:00:00',
	    payment_amount: '500000',
            status : '1'
	}
$scope.postData = function() {
	var post = new Post($scope.dummy);
	post.$save();
}

The problem is the data doesn’t seem to be saved on my database. I’ve tested my API using Wiztools RESTclient and it worked just fine so i know that the problem is on the client side.
Thanks.


#2

Maybe it’s just in this message but there’s a missing paren in your postData definition, it should be:

$scope.postData = function() {
	var post = new Post($scope.dummy);
	post.$save();
}

Then maybe the problem is in the way you call postData().


#3

sorry it’s just a typo when i copied the code. There are no any errors when i run the program


#4

OK but how do you call postData? So far you showed only how you defined it.


#5

I just call it from a button like

<button class="button button-block button-assertive" ng-click="postData()">
	Submit
</button>

#6

Are you sure your API expects data sent in JSON and not in x-www-form-urlencoded?


#7

I tried to encode my json to x-www-form-urlencoded but still not working

$scope.jsonToURI = function(json)
{ 
	return encodeURIComponent(JSON.stringify(json)); 
}



$scope.postData = function() {
     var post = new Post($scope.jsonToURI($scope.dataTobesent));
         post.$save();
}

my new service

.factory('Post', function($resource) {
	 return $resource('http://192.168.137.1/MYWEB/api/Myserver/order',{},{
	 query: {
		 method:'POST', 
		 headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
	}
 });

});

it returns no error in console.
However when i tried to use $http it returns failed

        $scope.dummy = encodeURIComponent($scope.dummy); 
	$http({
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		url: 'http://192.168.137.1/MYWEB/api/Myserver/order',
		method: "POST",
		data: $scope.dummy 
	})
	.then(function(response) {
		//return succes
	}, 
	function(response) { // optional
		//return failed
	});

#8

Did you print the response when it fails?


#9

Yes, and this makes me confused

Object { data: "<!DOCTYPE html> <html lang="en"> <h…", status: 500, headers: headersGetter/<(), config: Object, statusText: "Internal Server Error" }

when i test it with WizTools RESTclient, the data is saved in the DB and the response is

HTTP/1.1 201 Created

That RESTclient tool uses “application/x-www-form-urlencoded; charset=UTF-8”


#10

For an error 500, the only way to understand what is going on is to look at the server logs hoping that the error has been logged with more details. If you wrote the server it’s even easier.