有一个node.js服务器,当客户端使用websocket与node.js服务器连接时,我将调用我的web api控制器。林'使用socket.io将客户端与node.js服务器连接,然后node.js服务器向web api控制器发送xhr POST请求。但得到的回应是, " {"消息":"未找到与请求URI匹配的HTTP资源' http://localhost:4928/api/Player'。"," ; MessageDetail":"未在控制器上找到任何操作'播放器'与请求匹配。"}"
Web API方法
public class PlayerController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
public string Post(string value)
{
return value;
}
}
Node.js的
var app = require('express')();
var http = require('http').Server(app);
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var querystring = require('querystring');
var io = require('socket.io')(http);
io.on('connection', function (socket) {
console.log('A user connected ' + socket.handshake.address);
PostRequest(socket.handshake.address);
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected ' + socket.handshake.address);
});
});
function PostRequest(data) {
var url = "http://localhost:4928/api/Player";
var params = "value=" + data;/* querystring.stringify({ "value": data })*/
xhr.open('POST', url, true);
xhr.onreadystatechange = function () {//Call a function when the state changes.
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
console.log(xhr.responseText);
}
xhr.send(querystring.stringify({ value: "sss" }));
}
http.listen(3000, function () {
console.log('listening on *:3000');
});
无论如何,当我调用get方法发送xhr get请求时,这是有效的
答案 0 :(得分:0)
您需要通知Web API查找值 parameter in the Request body,因为您没有通过查询字符串发送数据。尝试修改Post动作签名:
public string Post([FromBody]string value)
{
...
}