简体   繁体   中英

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.

Here's 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.

if you are hitting the url with config option like this

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.

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

在此处输入图片说明

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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