简体   繁体   English

如何从角度$ http发布请求获取json字符串

[英]how to get json string from angular $http post request

I am sending this POST, I want to see the string that get's sent in the request before I send it. 我正在发送此POST,我想查看发送之前在请求中发送的字符串。

Here's Plunker 这是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.
  });

I guess what I am trying to do is see the JSON string being sent to the server. 我想我想做的是看到JSON字符串被发送到服务器。

if you are hitting the url with config option like this 如果您正在使用config选项访问网址,例如

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);
});

Then you can see the data what are send. 然后,您可以查看发送的数据。 Open your browser console and click network then under network click your url what you have hit And now see the look at Request Payload under header tab There your data will be revealing what you have send to server. 打开浏览器console ,单击“ network然后在networkclick您的URL,然后click Request Payload under header tab数据将显示您已发送到服务器的内容。

And if you are not using config option like this 如果您不使用这样的配置选项

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

Then do the same as above only difference is where you have seen the data under Request Payload , now you will see the same data under Form Data 然后执行与上述相同的操作,唯一的区别是您在Request Payload下看到了数据,现在您将在Form Data下看到相同的Form Data

在此处输入图片说明

在此处输入图片说明

From what I understand, I think you want different handlers if the request succeeds or fails. 据我了解,我认为您希望请求成功或失败的处理程序不同。 You can do it in this way: 您可以通过以下方式进行操作:

$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.
});

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

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