开发者

How to get Wireit json output?

开发者 https://www.devze.com 2023-04-05 01:36 出处:网络
I am testing wireit and am able to create new form containers and such.I tested the ajax adapter example and have it configed something like:

I am testing wireit and am able to create new form containers and such. I tested the ajax adapter example and have it configed something like:

WireIt.WiringEditor.adapters.Ajax.config =  {
    saveWiring: {
        method: 'PUT',
        url: 'http://voipd7.localhost/wirings.json'
    },
    deleteWiring: {
        method: 'GET',
        url: function(value) {
            if(console && console.log) {
                console.log(value);
            }
            // for a REST query you might want to send a DELETE /resource/wirings/moduleName
            return "fakeSaveDelete.json";
        }
    },
    listWirings: {
        method: 'GET',
        url: 'listWirings.json'
    }

The save url "http://voipd7.localhost/wirings.json" is a php page that just writes the $_GET or $_Post to file, but the only thing that it outputs is:

Arra开发者_高级运维y
(
    [q] => wirings.json
)

Am I missing something? Shouldnt this be sending json stuff via get or post?


From the provided configuration, it is evident that the HTTP method for doing a save operation is PUT. When using this method, the superglobal variables $_POST or $_REQUEST do not contain the posted data. $_GET will still have the data from the query string.

To parse the posted data when the method is PUT, do some of the following:

$vars = array();
parse_str(file_get_contents("php://input"), $vars);

The PUT method expects to receive file as input. That is why we use file_get_contents and parse_str parses the argument as if it were the query string passed via a URL. To extract the data variables regardless of the method use:

$vars = array();
if($_SERVER['REQUEST_METHOD'] == 'GET') {
    $vars = $_GET;
} else if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $vars = $_POST;
} else if($_SERVER['REQUEST_METHOD'] == 'PUT') {
    parse_str(file_get_contents("php://input"), $vars);
}

I based the reply on this very nice article: http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

One note: usually the web server is configured not to allow PUT methods but when it sees (at least Apache does this) that the PUT request is for an existing file, it calls this file to handle the request and the above code can be used in the file to extract the sent parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消