Closed fi5u closed 9 years ago
req = {
method: 'POST',
url: CONSTANTS.external.server + '/slim/api/posts',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify({
'abc': '123',
'def': '456'
})
}
try to stringify your object with json and send it as plain text. so your request will look like this:
POST /slim/api/posts HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 24
Connection: keep-alive
{"abc": 123, "def": 456}
Thanks, but it seems like when I try to pass any data, I get a 404 error with: XMLHttpRequest cannot load http://dev.site/slim/api/posts
. If I don't pass the data, I don't get the error.
Any ideas what might be causing this issue?
In web master tools when you do your request and you don't get 404 what TYPE it says ? POST
or GET
. Maybe your XMLHttpRequest
wrapper when it has an empty body is using GET
?
It was going through as POST
but I found that it didn't seem to like the header content type application/json
. So I sent it as text/plain
and I no longer get the error. However, I'm also still not able to process the data. Checking the Slim documentation again it seemed the code in $slim->post()
was wrong. The following is what I currently have, but it just returns a 500
error.
public function slim_mapping($slim) {
$context = $this;
$slim->post('/slim/api/posts', function()use($context) {
$data = $slim->request->getBody();
$context->addPost($data);
});
}
and you checked the Apache errors.log and found no clues why you get that 500 response ? and what do you do with data ? do you json_decode
it ?
Nothing is logged to Apache error log when the request is made.
I'm almost certain the problem lies with how I'm getting the data within the PHP code. If I replace the line
$data = $slim->request->getBody();
in slim_mapping()
with
$data = 'abc'
then abc
is returned back to the JavaScript calling function with a 200
response. This says to me that the above line is producing an error. Is it correct that it is $slim->request->getBody()
?
yes you are right, to have access to the $slim
object in the callback function you need to add it to the use($context, $slim)
and now you have access to $slim
object.
public function slim_mapping($slim) {
$context = $this;
$slim->post('/slim/api/posts', function()use($context, $slim) {
$data = $slim->request->getBody();
$context->addPost($data);
});
}
Yes, that works! Many thanks for working through it with me.
Hi, I'm just getting set up with this plugin and I'm wondering how I pass Post data through and then use it from the
slim_mapping
function.I'm querying the api with javascript, (specifically Angular):
Then in the PHP file, the
slim_mapping
function looks like:How do I get hold of the passed data within the
data
object?