Codeigniter + Angular Js: How to receive JSON data

后端 未结 5 1147
盖世英雄少女心
盖世英雄少女心 2020-12-16 20:24

This is the situation:

I have a simple app made in Angular JS that comunicate with the server through an API made in codeigniter.

There is a

5条回答
  •  余生分开走
    2020-12-16 20:47

    Although this is already answered I quite often have this quick little utility included in many of my applications that need to accept both application/x-www-form-urlencoded and application/json POSTs.

    if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') === 0 && stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE) {
        // POST is actually in json format, do an internal translation
        $_POST += json_decode(file_get_contents('php://input'), true);
    }
    

    After that point you can now just use the $_POST superglobal as you normally would, all the JSON data will be decoded for you.

提交回复
热议问题