问题描述
首先,我会说我在Javascript/React方面有点不适应.我正在尝试与WCF终结点服务器通信,但似乎没有得到响应就无法发送任何POST消息:
I'll start by saying I'm a bit of a newb when it comes to Javascript/React. I am attempting to communicate with my WCF endpoint server but I can’t seem to send any POST messages without getting a response:
OPTIONS http://###/testbuyTicket 405(不允许使用方法)
OPTIONS http://###/testbuyTicket 405 (Method Not Allowed)
似乎是因为我使用内容类型JSON发送它,因此需要进行预检",这就是失败的原因.
It seems that because I am sending it with content-type JSON it requires a ‘pre-flight’ and this is where it is failing.
这是我的客户代码:
var headers = {
'headers': {
'Content-Type': 'application/json',
}
}
axios.post(call, data, headers).then(res => {
try {
if (res) {}
else {
console.log(res);
}
}
catch (err) {
console.log(err);
}
}).catch(function (error) {
console.log(error);
});
这是错误详细信息:
我不知道为什么这次预检失败了.在服务器上,我已经允许了所有我认为需要的东西:
I don’t see why this pre-flight is failing. On the server I have already allowed everything I believe I need:
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS"},
{"Access-Control-Allow-Headers", "X-PINGOTHER,X-Requested-With,Accept,Content-Type"}
[ServiceContract]
public interface IPlatform
{
[OperationContract]
[WebInvoke(UriTemplate = "testbuyTicket")]
TicketResponse TestBuyTicket(PurchaseRequest purchaseRequest);
}
任何帮助将不胜感激.我觉得我已经尝试了一切.预先感谢.
Any help would be appreciated. I feel like I've tried everything. Thanks in adance.
推荐答案
我找到了一个解决方案,我不确定这是否是最优雅的解决方案,但是它确实有效.
I have found a solution, I'm not sure if it's the most elegant solution but it does work.
基本上,我也有一个端点,该调用也应定向,但是它仅接受POST请求,因此我添加了一个带有空方法的OPTIONS端点,并且现在看来一切正常.
Basically I have an endpoint that the call should be directed too, but it only accepts POST requests, so I have added an OPTIONS endpoint with an empty method and it all appears to work now.
所以我现在有:
[ServiceContract]
public interface IPlatform
{
[OperationContract]
[WebInvoke(UriTemplate = "testbuyTicket")]
TicketResponse TestBuyTicket(PurchaseRequest purchaseRequest);
[OperationContract]
[WebInvoke(UriTemplate = "testbuyTicket", Method = "OPTIONS")]
TicketResponse TestBuyTicketOptions(PurchaseRequest purchaseRequest);
}
这样做允许服务器响应OPTIONS调用,然后响应POST调用.
Doing this allows the server to respond to the OPTIONS call and then the POST call.
感谢大家的帮助.
向@demas大喊大叫,请参阅帖子飞行前响应具有无效的HTTP状态代码405 以获取更多信息
Big shoutout to @demas for the idea, see post Response for preflight has invalid HTTP status code 405 for more info
这篇关于飞行前失败并显示状态405的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!