gpt4 book ai didi

javascript - 尝试处理 CORS 请求时出现 ParseError

转载 作者:行者123 更新时间:2023-11-27 23:22:28 27 4
gpt4 key购买 nike

我正在尝试从静态页面向 Rails API 进行 API 调用。它们托管在不同的域上,因此我需要启用 CORS - 它可以是预检请求或简单的 CORS 请求。

我收到的错误是ActionDispatch::ParamsParser::ParseError (399: 'object Object]'处出现意外标记)。我不知道这是怎么发生的。

我的 Rails API 代码:

Controller .rb:

class VisitorsController < ApplicationController

skip_before_filter :verify_authenticity_token

before_filter :set_headers
after_filter :cors_set_access_control_headers

def create
puts 'VisitorsController#create'
@visitor = Visitor.new(visitor_params)

if @visitor.save
render json: @visitor, status: :created
else
render json: @visitor.errors, status: :unprocessable_entity
end
end

private
def visitor_params
params.permit(:email, :phone)
end

def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
headers['Access-Control-Max-Age'] = '1728000'
end

def set_headers
puts 'set_headers'
if request.method == 'OPTIONS'
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token, Content-Type'
headers['Access-Control-Max-Age'] = '1728000'

render :text => '', :content_type => 'text/plain'
end
end
end

routes.rb:

match 'visitors', to: 'visitors#create', via: [:options, :post]

上述设置(或类似的设置)确实适用于较旧的项目,并且也与此 gist 一致。 。我觉得错误出在客户端代码中,所以我尝试了不同的方法:

script1.js:

            var url = "http://localhost:3000/v1/visitors/";
var method = "POST";
var postData = {email: email, phone: phno};

var async = true;

var request = new XMLHttpRequest();
request.onload = function () {
// You can get all kinds of information about the HTTP response.
var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
var data = request.responseText; // Returned data, e.g., an HTML document.
console.log("response " + data);
};

request.open(method, url, async);

request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.send(postData);

script2.js:

            $.ajax({
url: 'http://localhost:3000/v1/visitors/',
type: 'POST',
data: {email: email, phone: phno},
crossDomain: true,
dataType: "json",
contentType: 'application/json; charset=utf-8',
headers: {"X-Requested-With": "XMLHttpRequest"},
error: function (xhr) {
console.log('Error: ' + xhr.statusText);
},
success: function (result) {
// do something
},
async: true,
processData: false
});

但是在这两种情况下我都会遇到相同的错误:

XMLHttpRequest cannot load http://localhost:3000/v1/visitors/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 400.

服务器日志中的错误:

Started POST "/v1/visitors/" for 127.0.0.1 at 2016-02-08 17:49:43 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Error occurred while parsing request parameters.
Contents:

[object Object]

ActionDispatch::ParamsParser::ParseError (399: unexpected token at 'object Object]'):
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:53:in `rescue in parse_formatted_parameters'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:32:in `parse_formatted_parameters'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:23:in `call'
activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
puma (2.16.0) lib/puma/server.rb:557:in `handle_request'
puma (2.16.0) lib/puma/server.rb:404:in `process_client'
puma (2.16.0) lib/puma/server.rb:270:in `block in run'
puma (2.16.0) lib/puma/thread_pool.rb:106:in `call'
puma (2.16.0) lib/puma/thread_pool.rb:106:in `block in spawn_thread'


Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.4ms)
Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.6ms)
Rendered /home/vedant/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (10.6ms)

最佳答案

看来这是一个真正的菜鸟错误。我发送了不正确的 json 数据。我所要做的就是改变

postData = {email: email, phone: phno};

postData = JSON.stringify({email: email, phone: phno});

使用客户端script1.js一切正常。

希望此 QnA 可以为尝试对 Rails api 进行 CORS 调用的任何人提供引用。

关于javascript - 尝试处理 CORS 请求时出现 ParseError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35270063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com