Coder Social home page Coder Social logo

Using post data about wp-slim-framework HOT 8 CLOSED

botnary avatar botnary commented on August 11, 2024
Using post data

from wp-slim-framework.

Comments (8)

Botnary avatar Botnary commented on August 11, 2024
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.

fi5u avatar fi5u commented on August 11, 2024

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.

Botnary avatar Botnary commented on August 11, 2024

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.

fi5u avatar fi5u commented on August 11, 2024

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.

Botnary avatar Botnary commented on August 11, 2024

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.

fi5u avatar fi5u commented on August 11, 2024

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.

Botnary avatar Botnary commented on August 11, 2024

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.

fi5u avatar fi5u commented on August 11, 2024

Yes, that works! Many thanks for working through it with me.

from wp-slim-framework.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.