【问题标题】:how to get json string from angular $http post request如何从角度 $http 发布请求中获取 json 字符串
【发布时间】:2015-09-03 05:01:44
【问题描述】:

我正在发送这个 POST,我想在发送之前查看请求中发送的字符串。

这里是Plunker

$http.post('/someUrl', {msg:'hello word!'}).
  then(function(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

我想我想要做的是查看正在发送到服务器的 JSON 字符串。

【问题讨论】:

  • 不清楚。你想说什么?
  • 摸摸,现在清楚了吗?

标签: javascript json angularjs post request


【解决方案1】:

如果您使用这样的配置选项点击 url

var config = {
    headers: { 'Content-type': 'application/json' },
    'dataType': 'json'
 };
var data = {
    name: 'intekhab',
    age:26,
};
$http.post('/admin/header', data, config).then(function(response){
    console.log(response);
});

然后您可以看到发送的数据。打开您的浏览器console 并点击network 然后在network click 你的网址下你点击了什么 现在看看Request Payload under header tab 您的数据将显示您发送到服务器的内容。

如果你不使用这样的配置选项

var data = {
        name: 'intekhab',
        age:26,
    };
    $http.post('/admin/header', data).then(function(response){
        console.log(response);
    });

然后和上面一样,唯一的区别是你在Request Payload下看到的数据,现在你将在Form Data下看到相同的数据

【讨论】:

  • 如果还是看不到请告诉我
  • 谢谢,我不知道chrome的功能。我可以看到正在完成的任何请求的所有数据。
【解决方案2】:

据我了解,如果请求成功或失败,我认为您需要不同的处理程序。你可以这样做:

$http.post('/someUrl', {msg:'hello word!'}).
  success(function(response) {
    // this callback will be called asynchronously
    // when it succeeds
  }).error(function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多