axios POST 请求 (javascript) 和 Sinatra/Base API (ruby) 的问题
Issues with axios POST requests (javascript) and Sinatra/Base API (ruby)
我一直在尝试使用 javascript 库 axios
向我的 Ruby Sinatra/Base API 发出 POST 请求时遇到问题
我在下面的 Sinatra API 中有一个示例 POST 路由,axios 一直给我一般性错误
# Running on http://localhost:9292
class Endpoints < Sinatra::Base
register Sinatra::MultiRoute
before do
headers 'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => ['Content-Type']
end
options '*' do
headers 'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => ['Content-Type']
end
route :post, :options, '/create' do
# does something with params and return a JSON object
end
end
我的 Javascript 使用 axios 库的代码:
// Running on http://localhost:8080
axios.post('http://localhost:9292/create', {
some_val: 'some value'
})
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
我的控制台中不断出现一般性 javascript 错误
POST http://localhost:9292/create 403 (Forbidden) bundle.js:20263
Error: Request failed with status code 403
at createError (bundle.js:12159)
at settle (bundle.js:19627)
at XMLHttpRequest.handleLoad (bundle.js:11996)
我的服务器端终端没有给我任何更好的工作方式,它说选项通过 200 状态代码传递,但没有给我任何导致 403 错误的原因...没有参数使它成功进入我的路线...
::1 - - [08/May/2017:12:49:35 -0700] "OPTIONS /create HTTP/1.1" 200 - 0.0030
::1 - - [08/May/2017:12:49:35 -0700] "POST /create HTTP/1.1" 403 30 0.0076
嗯,谢谢,这个解决方法对我有用:
before do
if request.request_method == 'POST'
body_parameters = request.body.read
begin
data= params.merge!(JSON.parse(body_parameters))
@can_parse = true
rescue
puts "LOG: cant parse params" #TODO add real logger
@can_parse = false
end
end
我一直在尝试使用 javascript 库 axios
向我的 Ruby Sinatra/Base API 发出 POST 请求时遇到问题我在下面的 Sinatra API 中有一个示例 POST 路由,axios 一直给我一般性错误
# Running on http://localhost:9292
class Endpoints < Sinatra::Base
register Sinatra::MultiRoute
before do
headers 'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => ['Content-Type']
end
options '*' do
headers 'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => ['Content-Type']
end
route :post, :options, '/create' do
# does something with params and return a JSON object
end
end
我的 Javascript 使用 axios 库的代码:
// Running on http://localhost:8080
axios.post('http://localhost:9292/create', {
some_val: 'some value'
})
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
我的控制台中不断出现一般性 javascript 错误
POST http://localhost:9292/create 403 (Forbidden) bundle.js:20263
Error: Request failed with status code 403
at createError (bundle.js:12159)
at settle (bundle.js:19627)
at XMLHttpRequest.handleLoad (bundle.js:11996)
我的服务器端终端没有给我任何更好的工作方式,它说选项通过 200 状态代码传递,但没有给我任何导致 403 错误的原因...没有参数使它成功进入我的路线...
::1 - - [08/May/2017:12:49:35 -0700] "OPTIONS /create HTTP/1.1" 200 - 0.0030
::1 - - [08/May/2017:12:49:35 -0700] "POST /create HTTP/1.1" 403 30 0.0076
嗯,谢谢,这个解决方法对我有用:
before do
if request.request_method == 'POST'
body_parameters = request.body.read
begin
data= params.merge!(JSON.parse(body_parameters))
@can_parse = true
rescue
puts "LOG: cant parse params" #TODO add real logger
@can_parse = false
end
end