在后Restangular不设置页眉 [英] Restangular not setting headers on post

查看:212
本文介绍了在后Restangular不设置页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置使用restangular一个POST请求头,但该请求被发送以纯文本格式,而不是JSON。
我已阅读文档这里以及一<一href=\"http://stackoverflow.com/questions/21292052/setting-headers-and-http-params-for-a-single-request-in-restangular\">similar问题。
我按照prescribed方法,但都无济于事。

I am trying to set the header for a single post request using restangular but the request is being sent as plain text instead of json. I have read the documentation here as well as a similar question. I have followed the prescribed methods but to no avail.

在服务器端,我用防爆preSS 4.x和使用MongoDB的所谓Moongoose一个ORM通信。

On the server side am using Express 4.x and communicating with Mongodb using an ORM called Moongoose.

服务器端:使用前press.js server.js 4.0

'use strict';

var express = require('express'),
    morgan  = require('morgan'),
    port =process.env.PORT || 3000,
    bodyParser  = require('body-parser'),
    methodOverride = require('method-override'),
    app = express();


app.use(morgan('dev'));             
app.use(bodyParser());              
app.use(methodOverride());          
app.use(express.static(__dirname+"/app"));

require('./config/models');
require('./config/user_ctrl');
require('./config/comt_ctrl');
require('./config/routes')(app);

app.listen(port);   
console.log('Magic happens on port: '+port); 

//Server side: routes.js
var pips = require('./user_ctrl');

module.exports = function(app){

    app.get('/api/pipplez',pips.getPipplez); //Gets all users in collection
    app.post('/api/pipplez/wan',pips.getPipplezById); //Gets the specified user

    app.all('/api/*', function(req, res) {
        res.send(404);
    });

    app.get('/*', function(req, res){
       res.send('index.html'); 
    });
};

在客户端我有这个。

客户端:配置后app.js

Client side: app.js after config

.factory('userServ',function(Restangular){

  var ol = Restangular.withConfig(function(conf){
    conf.setBaseUrl('/api/')
  });

  var an = Restangular.withConfig(function(conf){
    conf.setBaseUrl('/api/users/')
  });
    return{
          oldem: ol.one('users').getList(),
          wandem: an.one('one')
    }
});

客户端:userCtr.js

Client side: userCtr.js

'use strict';

ting.controller('userCtrl', function($scope, userServ){

    $scope.pip = {
       name: ''
    };

    $scope.getOlPips = function(){

    userServ.oldem.then(function(rez){

        if(rez!=='null'){
            $scope.dem = rez;
            console.log($scope.dem);
        }
    }, function(err){
        console.log('Error!!!\n', err );
    })  
};

$scope.getWanPip = function(pip){
    //console.log(pip);
    RestServ.wandem.post(pip, {}, {'Content-Type':'application/json'}).then(function(res){
        console.log(res)      
    }, function(err){
        console.log('Error!!!\n', err);
    })
};

$scope.getOlUzas();    
});

在HTML的一部分

part of the html

<form>
    <input ng-model='pip.unm' maxlength= '20' placeholder='Search for user...'>
    <button class="btn btn-primary" ng-click = 'getWanPip(pip)'>Find</button>
</form>

我已经广泛使用后端<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en\"相对=nofollow>邮递员。后端运行正常。该应用程序能够从数据库中获取所有记录但是当我做POST请求,我收到了404,因为请求的格式为纯文本,而不是JSON发送。我怎样才能得到这个工作?

I have extensively tested the back end using postman. The back end is functioning. The application is able to get all the records from the database however when I make the post request I get a 404 because the format of the request is sent as plain text instead of json. How can I get this to work?

推荐答案

在网络上摸索后,我发现restangular默认表单数据发送到服务器的JSON格式。笔者从铬的开发者工具误导。为了disect http请求我建议小提琴手
如果一个人想格式化restangular头发送从形式提出的请求,你可以做到以下几点:

After groping around the web, I found that restangular sends form data to the server in json format by default. I was misguided by the developer tools from chrome. In order to disect http requests I recommend fiddler If one wants to format the header in restangular being sent as a request from the form you can do the following:

//From the previous example....
//Assuming restangular did not not send form data in json format by default

    $scope.getWanPip = function(pip){

        RestServ.wandem.post(pip, {},{}, {'Content-Type':'application/json'}).then(function(res){
            console.log(res)      
        }, function(err){
            console.log('Error!!!\n', err);
        })

请注意两个空的大括号,而不是一...
我仍然不能执行职务,但,这是因为数据正在而不是单独发送的URL的一部分。这就需要另外一个问题...

Note the two empty curly braces instead of one... I can't still perform the post but this is because the data is being sent as part of the url instead of separately. This calls for another question...

这篇关于在后Restangular不设置页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆