[英]Need help codeigniter restclient with node server
我在codeigniter中有一個休息客戶端,在節點中有一個服務器端。 Codeigniter Rest Client的源鏈接我的客戶端Rest控制器如下所示
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 // "
我要去哪里錯了?
將CI控制器中的發布功能更改為
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.