Request Payload vs. Form Data
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.
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.
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.
Hi @eMarek, please send me a screenshot of request headers. Thank you.

OK, try to add 'json' flag into the route.
Example:
framework.route('/login/json/', json_login, ['json']);
Please give me a feedback.
Niceeee, thank you, this solved my problem.
Can I ask what is the difference between using json or xhr at routing?
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 What are the pros & cons of using JSON flag vs XHR flag?
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.
Hii, Can anyone tell me how to insert image file in the request payload which is stored in local file system??? Thanks in advance.
HI @sahilsethi16, did you mean from the server-side or client-side?
Thanks.
E.g. from client-side: https://github.com/petersirka/jquery.upload
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?
Hi when I send form as form data,like "key":"value". but,how to send my array,like "skills": [[{id:1,name:"tom"}]]
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?
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:

@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.

