我在codeigniter中有一个rest客户端,在节点中有一个服务器端。 The source link for codeigniter rest client 我的客户端休息控制器看起来像这样
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->spark('restclient/2.1.0');
$this->load->library('rest');
$this->rest->initialize(array('server' => 'http://localhost:3000/'));
}
public function post() {
$param = array(
'var1'=>1,
'var2'=>2
);
$this->rest->format("application/json");
$this->rest->post("/", $param);
var_dump($this->rest->get("/"));
}
}
在服务器端我有
var express = require('express');
var bodyParser = require("body-parser");
var app = express();
var port = 3000;
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/", function (req, res) {
var query1 = req.body.var1;
var query2 = req.body.var2;
res.json({result: parseInt(query1) + parseInt(query2)});
});
app.listen(port);
但是当我被请求时localhost/welcome/post
我得到了resposne
string(14) "Cannot GET // "
我哪里错了?
答案 0 :(得分:0)
将CI控制器中的post函数更改为
public function post() {
$param = array(
'var1'=>1,
'var2'=>2
);
$this->rest->format("application/json");
//since the server url is allready mentioned as http://localhost:3000/
// no need to mentin "/" again
// response should be handled by a variable
$response = $this->rest->post("", $param);
var_dump($response);
}