简体   繁体   English

使用angularjs / javascript将JSON对象传递到服务器

[英]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)? 如何将信息从前端的输入字段(使用angularjs)作为JSON对象传递到服务器(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>`

Just construct your JSON payload and pass it to $http as the second param 只需构造您的JSON有效负载并将其传递给$http作为第二个参数

data: { query1: $scope.query1 }

and then 接着

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM