2015-09-19 53 views
1

我想发布使用角度,但不断得到404错误。它看起来好像没有提出请求。任何想法为什么?感谢所有的帮助。无法发布在角度/快递

var param = $scope.tag; 
// client 
    $http({method: 'Post', url: '/api', headers: { 
    "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }, data: {tag: param}}) 
    .then(function(response) { 
     console.log(response); 
    }); 

// server 
app.post('/api', function (req, res) { 
    var tag = req.body.tag; 
    request("http://food2fork.com/api/search?key=KEY&q=" + tag, function (req, res) { 
    res.json(response); 
    }); 
}); 

// request headers 
Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate 
Accept-Language:en-US,en;q=0.8 
Access-Control-Allow-Origin:* 
Cache-Control:no-cache 
Connection:keep-alive 
Content-Length:17 
Content-Type:application/json 
Host:localhost:3000 

// response headers 
Connection:keep-alive 
Content-Length:22 
Content-Type:text/html; charset=utf-8 
Date:Sat, 19 Sep 2015 23:22:15 GMT 
X-Content-Type-Options:nosniff 
X-Powered-By:Express 

回答

1

你不应该使用所有的代码。尝试:

// Client: 
var tag = $scope.tag; 

$http.post('/api', {tag: tag}) 
    .then(function(response) { 
    console.log(response); 
}); 

//Server: 
var bodyParser = require('body-parser'); 
app.use(bodyParser.json()); 

app.post('/api', function(req, res) { 
    var tag = req.body.tag; 
    res.send(tag); 
}); 
+0

我曾尝试你的代码,我得到这个错误:POST HTTP://本地主机:3000/API网:: ERR_CONNECTION_REFUSED 另外,我请求头表明这一点:临时列头显示 – Alex

1

使用method: 'POST'而不是method:'Post'

后者不被识别,而是发出默认的GET,而不是您期望的POST。

编辑:检查代码,我上面写的是不是真的

除此之外......不应该是这样

/api/tags 

一些网址???

+0

我改成了“POST”但我仍然收到404错误。这样做的目的是让我可以抓住参数并在服务器上使用它。 – Alex

+0

你可以从开发者工具发布请求信息吗? –

+0

您是否在寻找响应标题?我更新了帖子以显示两者。 – Alex