Anoop
Anoop

Reputation: 17

passing a JSON object to server using angularjs/javascript

How can I pass the information from an input field in the front end(using angularjs) as a JSON object to a server(javascript)?

Basically what I intend to do is take input from a user, and pass it as a query to search a database and return the values.

My controller code:

`app.controller('SearchController',['$scope','$http',function($scope,$http){
    $scope.click=function(){

    var data=$scope.query1;

    $http.post('/credjson',data);

    /*$scope.addRowAsyncAsJSON = function(){        
        $scope.cred.push({ 'query':$scope.query1 });

        var dataObj = {
            query : $scope.query1,
        };  

        var res = $http.post('/credjson', dataObj);
        res.success(function(data, status, headers, config) {
            $scope.message = data;
        });
        res.error(function(data, status, headers, config) {
            alert( "failure message: " + JSON.stringify({data: data}));
        });     

        $scope.query1='';*/`

Note: The code in between /* */ was a different attempt at trying this out.

My view code:

    `<div style="padding-top:20px" ng-controller="SearchController">
        <form name="form1" ng-submit="click()">
    <input id="creditq" ng-model='query1' type="text" /> 
    <button id="Search" ng-value='Search'>Search</button><br/><br/>
    </form>
    </div>`

Upvotes: 1

Views: 1268

Answers (1)

kachhalimbu
kachhalimbu

Reputation: 2200

Just construct your JSON payload and pass it to $http as the second param

data: { query1: $scope.query1 }

and then

$http.post('/credjson',data);

Upvotes: 1

Related Questions