I try to make some post test but doesn't works
a little example like this:
var testdata = {email: "[email protected]", password: "pass"}
m.request({
method: "POST",
url: '/user/login',
data: testdata,
}).then(function(data){
console.log(data);
});
in the server I never received the email and password.
please, help me.
Ivan, you are going to have to dig a little deeper, both with developer tools in the browser to verify what is sent and with logs and dumps on the server to verify what is received.
I can assure you that posting data does indeed work (and your example looks correct to me) -- I use (current) Mithril with POST (and PUT, PATCH, LINK, UNLINK) extensively and have not had any problem with the request content not arriving.
Hi Lawrence,
My backend respond correctly to postman and other front in jquery.
I don't know if I missing parameters in my mithril's request or something like that, in my backend I use phalconphp and works fine but not with mithril.
I can't see the parameters over $_POST or $this->request->getPost() with the mithril example,
Thanks for the quick response
In the jquery post i can see X-Requested-With:XMLHttpRequest in the headers, but this param isn't in mithirl post.
then in the jquery request i see that:
email:[email protected]
password:passwords
in mithril request i see that:
{email: "[email protected]", password: "passwords"}
By default, Mithril sends data in JSON format. It sounds like you're using PHP, so this link might be useful: http://stackoverflow.com/questions/18866571/receive-json-post-with-php
Hi Leo,
I think thats probably works fine but y cant modify the backend, I have other aplications with that backend.
I can modify mithril to send the information like jquery format ?
Thanks for your help.
Ivan,
Refer to the documentation for m.request():
http://mithril.js.org/mithril.request.html
particularly
http://mithril.js.org/mithril.request.html#using-different-data-transfer-formats
Hi Ivan.
You can add that header using the config option for m.request like this:
m.request({
method: "POST",
url: '/user/login',
data: testdata,
config: function(xhr) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
}
}).then(function(data){
console.log(data);
});
Hi Porsager, I try it and send the headers correctly,
But the request body still like a json element and not like post
Thanks you for your help!
Ah ok. Then you need to send it urlencoded. Mithril assumes JSON as that is what most people would use.
Try this instead:
var data = { 'email':'[email protected]', 'password': 'pass' }
m.request({
method: 'POST',
url: '/user/login',
data: data,
serialize: function(data) { return m.route.buildQueryString(data) },
config: function(xhr) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
}
}).then(function(response){
console.log(response);
});
By the way. This question would probably be better on gitter or google groups since it's not really an issue with mithril.
Sorry, I know at this moment it's not an issue.
Thanks.
Most helpful comment
Ah ok. Then you need to send it urlencoded. Mithril assumes JSON as that is what most people would use.
Try this instead:
By the way. This question would probably be better on gitter or google groups since it's not really an issue with mithril.