角度HTTP Post请求,有效负载嵌套在JSON对象中

角度HTTP Post请求,有效负载嵌套在JSON对象中,json,angularjs,node.js,http,cors,Json,Angularjs,Node.js,Http,Cors,我正在学习Angular和Node,我正在尝试弄清楚如何让我的Angular应用程序运行一个单独的应用程序,该应用程序承载一个RESTAPI 请求正文显示为 {{“name”:“test”}:''} 我希望它能显示为 {“name”:“test”} 这是发送post请求的前端应用程序 $http({ method: 'POST', url: 'http://localhost:8080/api/test', data: { "name": 'test' }, data

我正在学习Angular和Node,我正在尝试弄清楚如何让我的Angular应用程序运行一个单独的应用程序,该应用程序承载一个RESTAPI

请求正文显示为
{{“name”:“test”}:''}
我希望它能显示为
{“name”:“test”}

这是发送post请求的前端应用程序

$http({
  method: 'POST',
  url: 'http://localhost:8080/api/test',
  data: {
    "name": 'test'
  },
  dataType: "json",
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
它正在到达定义为的路线

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.all('/', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
 });
router.post('/test', function(req, res) {
    var name = req.body.name;
    console.log(req.body);
});

我希望问题在于内容类型是application/x-www-form-urlencoded,但我不知道如何允许cors使用application/json。

JSONP可以用于执行cors请求(这里有更多关于json/JSONP的信息),在AngularJS文档中-

感谢您的回复!这也奏效了。我阅读了更多关于CORS的内容,发现了预飞行的概念,并添加了一个函数,该函数将向应用程序/json请求发送200条回复,上述代码现在就可以工作了。@dberry2002然后您可以通过单击左侧的灰色检查来接受正确的答案-只要4分钟计时器启动,该函数就会执行。再次感谢:)