Comments (8)
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}
from wp-slim-framework.
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?
from wp-slim-framework.
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
?
from wp-slim-framework.
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);
});
}
from wp-slim-framework.
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 ?
from wp-slim-framework.
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()
?
from wp-slim-framework.
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);
});
}
from wp-slim-framework.
Yes, that works! Many thanks for working through it with me.
from wp-slim-framework.
Related Issues (9)
- Help starting HOT 10
- How can I use middleware for authentication? HOT 1
- CORS Headers and Content-Type settings HOT 3
- DI Container Clash HOT 5
- I'm getting the 404 but can't get an output HOT 3
- Ajax CORS HOT 1
- Slim Framework 3 HOT 2
- Using composer to install plugin HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-slim-framework.