小编Leo*_*rea的帖子

CORS和Access-Control-Allow-Header如何工作?

我正在尝试从domain.com向a.domain.com发出CORS请求POST.

我的javascript看起来像这样

$('#fileupload').fileupload({
  xhrFields: {
    withCredentials: true
  },
  dataType: 'json',
  url: $('#fileupload').data('path'),
  singleFileUploads: true,
  add: function(e, data){
    data.submit();
  }
});
Run Code Online (Sandbox Code Playgroud)

首先,我看到OPTIONS路由被调用如下:

Request URL: https://a.domain.com/some/route
Request Method:OPTIONS
Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)

选项要求:

Access-Control-Request-Headers:origin, content-type, accept
Access-Control-Request-Method:POST
Host:a.domain.com
Origin:http://domain.com:3000
Referer:http://domain.com:3000/home
Run Code Online (Sandbox Code Playgroud)

选项响应

Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:POST
Access-Control-Allow-Origin:http://domain.com:3000
Connection:keep-alive
Content-Length:0
Content-Type:text/html;charset=utf-8
Run Code Online (Sandbox Code Playgroud)

这个要求回来了200个说明.在我的服务器上,我有相同的路由POST方法,这是我得到的回报之后OPTIONS

Request URL:https://a.domain.com/some/route
Run Code Online (Sandbox Code Playgroud)

发出请求

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryjwr5Pk7WBcfzMdbO
Origin:http://domain.com:3000
Referer:http://domain.com:3000/home
Run Code Online (Sandbox Code Playgroud)

并且POST请求被取消/失败.

我的问题是,我是否还需要在POST控制器上拥有access-control-allow-origin?

我有一个用于授权的cookie,该cookie具有cookie,.domain.com该cookie在请求中被发送过一次并且现在没有被发送.知道为什么会这样吗?

javascript jquery cors

62
推荐指数
1
解决办法
14万
查看次数

用Ruby替换文件中的单词

我是Ruby的新手,我一直在尝试替换文件中的单词.代码如下:

File.open("hello.txt").each do |li|
  if (li["install"])
  li ["install"] = "latest"
  puts "the goal state set to install, changed to latest"
  end
end
Run Code Online (Sandbox Code Playgroud)

当put中的消息被打印一次时,该单词在该文件的那一行中不会变为"latest".谁能告诉我这里有什么问题?谢谢

ruby string ruby-on-rails file

5
推荐指数
2
解决办法
8005
查看次数

golang:“func() interface {}”和“func() *MyStruct”不兼容的类型如何?

假设我有代码,其中一个函数接受另一个作为参数:

type Person struct {
    Name string
}

func personBuilder() * Person {
    return &Person{Name: "John"}
}

func printRetrievedItem(callback func() interface {}){
    fmt.Print(callback());
}

func doStuff(){
    printRetrievedItem(personBuilder);
}
Run Code Online (Sandbox Code Playgroud)

这导致错误cannot use personBuilder (type func() *Person) as type func() interface {} in function argument。如果我将personBuilder返回类型更改为interface{},它可以工作,但在我正在处理的实际项目中,我希望有一个具体的类型来实现清晰的设计和 TDD 目的。

Go 是否支持这种方法签名泛化?如果您无法更改personBuilder部件(例如,您有很多返回不同类型结构的无参数函数,并且您想构建一个接受这些构建器中的任何一个作为参数的消费者函数),有什么解决方法?

go

5
推荐指数
1
解决办法
2467
查看次数

在 Go 中使用系统调用

我正在尝试制作一个非常简单的脚本,它将使用 Go 中的系统调用来拉动系统正常运行时间和其他一些事情。这是一些无法编译的示例代码:

package main
import "syscall"
func main(){ 
    var info1 = new(syscall.Sysinfo_t) 
    var info2 = syscall.Sysinfo_t{}
}
Run Code Online (Sandbox Code Playgroud)

它一直给我一个错误提示undefined: syscall.Sysinfo_t。怎么了?

system-calls go

5
推荐指数
0
解决办法
2685
查看次数

函数calcDecrement的返回类型中"()"的含义是什么?

我刚开始学习Swift并遇到了函数类型以及如何返回它们.我只是想知道()函数返回值的含义是什么calcDecrement?我知道函数返回对内部函数的引用,那么函数decrementer()返回类型是什么?

func calcDecrement(forDecrement total: Int) -> () -> Int {
    var overallDecrement = 0
    func decrementer() -> Int {
        overallDecrement -= total
        return overallDecrement
    }
    return decrementer
}

let decrem = calcDecrement(forDecrement: 30)
print(decrem())
Run Code Online (Sandbox Code Playgroud)

function swift

3
推荐指数
1
解决办法
49
查看次数

需要帮助使用Rails将数据库从开发迁移到测试

类似的问题已被问到很多.但是,我认为我的情况有点不同.我从Heroku(我在prod中使用sqlite3)向我的本地机器(使用PostgreSQL)中提取了一个数据库.prod db当时没有任何数据.

在那之后,我还没有进行任何db迁移来测试.现在我在dev db中有几个字段.但是,当我去测试控制台看看我有什么(引用User表)时,User.count返回0,但在dev中返回2个用户.

所以我做了rake db:migrate RAILS_ENV=test既没有错误信息也没有成功信息.我认为这是成功的,并检查测试数据库是否有这2个用户,但仍然没有.然后我试了一下bundle exec rake db:test:prepare.这是输出

**You have 4 pending migrations:

  20120415210441 CreateUsers

  20120418064930 AddIndexToUsersEmail

  20120419034627 AddPasswordDigestToUsers

  20120504031144 AddRememberTokenToUsers

Run `rake db:migrate` to update your database then try again.**
Run Code Online (Sandbox Code Playgroud)

解决办法是什么?

编辑: 仅供参考:我正在使用Windows我也运行rake db:migrate 并且我收到了此错误.

$ rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id"
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), …
Run Code Online (Sandbox Code Playgroud)

testing ruby-on-rails rails-migrations ruby-on-rails-3

2
推荐指数
1
解决办法
3141
查看次数

Hstore和Rails

我正在尝试使用最新版本的activerecord-postgres-hstore gem在Rails 3.2.9项目中使用Hstore,并且我在使用ActiveRecord提供的store_accessor获取Hstore中的值时遇到了一些麻烦.

我的模型看起来像这样:

class Person < ActiveRecord::Base
  serialize :data, ActiveRecord::Coders::Hstore
  attr_accessible :data, :name
  store_accessor :data, :age, :gender
end
Run Code Online (Sandbox Code Playgroud)

当我进入rails控制台时,我得到的是:

Loading development environment (Rails 3.2.9)
1.9.3-p327 :001 > p = Person.find(3)
  Person Load (9.8ms)  SELECT "people".* FROM "people" WHERE "people"."id" = $1 LIMIT 1  [["id", 3]]
 => #<Person id: 3, name: "Leo", data: {"age"=>"34", "gender"=>"male"}, created_at: "2012-12-07 15:01:53", updated_at: "2012-12-07 15:27:40"> 
1.9.3-p327 :002 > p.age
 => nil 
1.9.3-p327 :003 > p.age = "204"
 => "204" 
1.9.3-p327 :004 > p.save!
   (0.2ms) …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql activerecord ruby-on-rails-3.2 hstore

2
推荐指数
1
解决办法
2858
查看次数

如何在Ruby中为'每个循环'创建这个?

鉴于:

class Author < ActiveRecord::Base
  has_many :articles
end
Run Code Online (Sandbox Code Playgroud)

收到包含作者文章的JSON文件后,我想销毁作者数据库中的文章,但不包含在JSON文件中.如何为每个循环创建"?"?

article_collection = @author.articles.all
unless article_collection.blank?
  article_collection.each do |article_from_collection|
    # How can I check on the next line if it's id is included in the params?
    if article_from_collection.arti_id "is not part of" params[:author][:articles_attributes]
      article_from_collection.destroy
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我如何检查第5行,根据arti_id哪一项也包含在参数中,是否arti_id存在JSON输入中存在的文章?

我是否应该arti_id在参数中构建s 的集合,然后用于.include?查看数据库中的每篇文章,如果它在此集合中?

我尝试了以下两行.false无论文章是否包含在json中,第一个返回.第二行返回错误TypeError Exception: no implicit conversion of Symbol into Integer.

if params[:author][:articles_attributes].include? article_from_collection.arti_id
if params[:author][:articles_attributes][:arti_id].include? article_from_collection.arti_id
Run Code Online (Sandbox Code Playgroud)

params[:author] 返回类似于:

{ "some_other_attributes"=>[ {"key"=>"value", …
Run Code Online (Sandbox Code Playgroud)

ruby loops ruby-on-rails-4

2
推荐指数
1
解决办法
88
查看次数

has_one 关联在 rails 中创建多条记录

我有一个 User 模型和 Shop 模型。我想允许用户只创建一个商店。所以在我的用户模型中

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :terms_and_conditions, :acceptance => true
  has_one :shop
end
Run Code Online (Sandbox Code Playgroud)

我的商店模型看起来像这样

class Shop < ApplicationRecord
  has_many :products, dependent: :destroy
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

但是从控制台,如果我尝试为已经拥有一家商店的用户创建一家新商店,则没有错误并成功提交。

[
  #<Shop id: 1, name: "Rabin & Rose Shop", location: "Banepa Kavre Nepal", description: "oho k vhk kl;o jjio ko;k; jljlkj", rating: nil, delivery_service: true, user_id: 1, created_at: "2017-09-27 …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails rails-activerecord

2
推荐指数
1
解决办法
892
查看次数

Ruby:字符串不能被强制转换为fixnum

我创建了一个程序,接受学生姓名和每个学生的分数输入.然后将学生的姓名保存为csv文件,并将分数保存在单独的csv文件中.

接下来,我有一个程序从该csv文件中读取并将名称和分数存储回一个数组中.然后,我在阵列中搜索学生,如果找到,则显示学生和学生分数.

这是我遇到问题的地方.我想找到得分数组的总和,这样我就可以使用总和找到平均值.我尝试这样做并得到错误(见标题):

score.inject(0) {|sum,i| sum += i}
puts sum
Run Code Online (Sandbox Code Playgroud)

当我做一个得分时它会说:["100""100""98""87"]

是不是因为数字周围的引号而将我的分数读作字符串?如果是这样,我该如何解决这个问题呢?

ruby

1
推荐指数
1
解决办法
1468
查看次数

在golang中,当使用net/http时,如何使用和不使用尾随括号调用函数

在main函数中,我有一个gorilla mux路由器,以及一个处理路由的函数.

var router = mux.NewRouter()
func main() {   
    router.HandleFunc("/", ParseSlash)
    http.Handle("/", router)
    http.ListenAndServe(":8000", nil)
}
Run Code Online (Sandbox Code Playgroud)

和ParseSlash看起来像

const slash = `<h1>Login</h1>
<form method="post" action="/login">
  <label for="name">User name</label>
  <input type="text" id="name" name="name">
  <label for="password">Password</label>
  <input type="password" id="password" name="password">
  <button type="submit">Login</button>
</form>`

func ParseSlash(response http.ResponseWriter, request *http.Request)  {
    fmt.Fprintf(response, slash)
}
Run Code Online (Sandbox Code Playgroud)

但是,在main中,我们并没有将函数称为ParseSlash(),而是ParseSlash在函数内部router.HandleFunc().如果我们没有提供明确的函数,函数从何处获取参数?这种调用函数的方式是什么?

谢谢.

parameters http function go

1
推荐指数
1
解决办法
503
查看次数