framework icon indicating copy to clipboard operation
framework copied to clipboard

Request Payload vs. Form Data

Open eMarek opened this issue 13 years ago • 14 comments

I was used to jQuery in my apps and I was doing ajax request like this:

$('#btn-login').click(function() {
    $.ajax({
        type: 'post',
        url: '/login',
        data: {
            username: $('#username').val(),
            password: $('#password').val()
        },
        success: function(server) {
            alert('ok');
        },
        dataType: 'json'
    });
});

If I take a look on my ajax request in Chrome Developer Tools under Network tab, I see that username and password were sent like Form Data.

screen shot 2013-08-10 at 4 59 09 pm

Lately I am testing angular.js and a similar ajax request I do with $http service:

$scope.login = function() {
    $http.post('/login', {
        username: $scope.username,
        password: $scope.password,
    }).success(function() {
        alert('ok');
    });
}

I noticed the difference how the data were sent. In this case with angular username and password were sent like Request Payload.

screen shot 2013-08-10 at 5 01 11 pm

The question is how to get Request Payload data in partial.js? In the case with jQuery I simply do self.post.username and self.post.password. In the case with angular.js I am without any idea.

eMarek avatar Aug 10 '13 15:08 eMarek

Hi @eMarek, please send me a screenshot of request headers. Thank you.

petersirka avatar Aug 10 '13 17:08 petersirka

screen shot 2013-08-10 at 8 12 58 pm

eMarek avatar Aug 10 '13 18:08 eMarek

OK, try to add 'json' flag into the route.

Example:

framework.route('/login/json/', json_login, ['json']);

Please give me a feedback.

petersirka avatar Aug 10 '13 18:08 petersirka

Niceeee, thank you, this solved my problem. Can I ask what is the difference between using json or xhr at routing?

eMarek avatar Aug 10 '13 18:08 eMarek

Super.

XHR flag:

  • request header must contain = X-Requested-With with XMLHttpRequest value (classic web browser AJAX request)

JSON flag:

  • request body must contain a valid JSON string

You can use json and xhr flag together.

Thanks.

petersirka avatar Aug 10 '13 18:08 petersirka

@petersirka What are the pros & cons of using JSON flag vs XHR flag?

bitcity avatar Aug 08 '14 14:08 bitcity

Hi @addremove,

JSON - A content type of the request must be application/json XHR - Request must contain header X-Requested-With:XMLHttpRequest, this is a classic AJAX/XHR request

Thanks.

petersirka avatar Aug 08 '14 14:08 petersirka

Hii, Can anyone tell me how to insert image file in the request payload which is stored in local file system??? Thanks in advance.

sahilsethi16 avatar Mar 15 '16 12:03 sahilsethi16

HI @sahilsethi16, did you mean from the server-side or client-side?

Thanks.

E.g. from client-side: https://github.com/petersirka/jquery.upload

petersirka avatar Mar 16 '16 09:03 petersirka

I have to send some image in my ajax request to server. my question is how to insert it its request payload

snippet is

var settings = { "url": "https://servername.com/profiles/photo.do?key=a16895d8-2fe8-46d7-9658-8194d9b2ecc8", "method": "PUT", "headers": { "authorization": "Basic c2FoaWwuc2V0aGk6V2VsY29tZUAwNw==", "content-type": "image/jpeg" } }

Now how to insert a image in its payload?

sahilsethi16 avatar Mar 16 '16 09:03 sahilsethi16

Hi when I send form as form data,like "key":"value". but,how to send my array,like "skills": [[{id:1,name:"tom"}]]

EnhengBenben avatar Dec 07 '16 02:12 EnhengBenben

Hi there are multiple ways to send array to server. What have you tried so far? What didn't work for you? Have you tried JSON.stringify or jQuery.serialize?

molda avatar Dec 07 '16 06:12 molda

Hi, It's my first time to work with web service, so can anyone of you help me? I want to know how to consume a JSON web service? When I consulted the Developer tools in Chrome, I've found this: sans titre

mansouridonia avatar Jul 10 '17 11:07 mansouridonia

@mansouridonia do you want to call this web service in Total.js application? Can you write me on Total.js Messenger, it will be faster.

petersirka avatar Jul 10 '17 11:07 petersirka