我写了一个简单的 Seneca 插件,我最终会用它来监听 http 消息:
买家.js
module.exports = function buyer (){
this.add('role:buyer, action: acceptOffer', function(msg, respond){
respond(JSON.stringify({answer: msg.id}))
})
}
当我使用以下脚本运行它时:
index.js
require('seneca')()
.use(require('./designs/offer/roles/buyer.js'))
.listen()
我现在可以发送 POST 请求localhost:10101/act
并使用该插件:
curl -d '{"role": "buyer", "action": "acceptOffer", "id": "12"}' http://localhost:10101/act
{"answer":"12"}
这就是事情变得混乱的地方。我现在想通过 web 应用程序发出这个 http 请求,所以我使用 axios 从网页发出这样的请求:
应用程序.vue
<template>
<div id="app">
<button @click="sendQuery"></button>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'App',
data(){
return {
postBody: {
id: '12',
role: 'buyer',
action: 'acceptOffer'
}
}
},
methods: {
sendQuery: function(){
var body = JSON.stringify(this.postBody)
axios.post(`http://localhost:10101/act`, {
body: body
})
}
}
}
</script>
当我单击按钮发送请求时,我从浏览器控制台收到此错误消息(启用 CORS 后):
xhr.js?ec6c:178 POST http://localhost:10101/act 500 (Internal Server Error)
dispatchXhrRequest @ xhr.js?ec6c:178
xhrAdapter @ xhr.js?ec6c:12
dispatchRequest @ dispatchRequest.js?c4bb:59
Promise.then (async)
request @ Axios.js?5e65:51
Axios.(anonymous function) @ Axios.js?5e65:71
wrap @ bind.js?24ff:9
sendQuery @ App.vue?26cd:26
boundFn @ vue.esm.js?efeb:190
invoker @ vue.esm.js?efeb:2004
fn._withTask.fn._withTask @ vue.esm.js?efeb:1802
createError.js?16d0:16 Uncaught (in promise) Error: Request failed with status code 500
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
谁能告诉我为什么这与 curl 的工作方式不同?为什么我得不到我的回应?