小编Hom*_*man的帖子

角$ http.get不会从Rails中获取json response_to(不调整标题)?

我在Rails应用程序上使用angularjs。我想制作一个分页服务器端数据的搜索表单。

这是我的表格:

<input type="text" class="search-query" placeholder="Search Products" ng-model="search_terms" />

我的rails控制器接受json或html请求:

def index
  # ....
  respond_to do |format|
    format.json {
      # ...
    }
    format.html {
      # ...
    }
  end
end
Run Code Online (Sandbox Code Playgroud)

这是我的角度控制器。由于某种原因,$ http服务只能获取Rails默认的html响应,而无法获取json响应。

$scope.$watch('search_terms',function(newValue,oldValue){
  var query = newValue;

  $http.get('/products',{params:{search_terms:query}, responseType:'json'}).success(function(data) {
  // do something
  });

});
Run Code Online (Sandbox Code Playgroud)

如果我使用此jQuery.get,我确实会获取所需的json响应,那么为什么angular会给我html?

  jQuery.get('/products',{search_terms:query},function(data){
     console.log(data);
  }, 'json')
Run Code Online (Sandbox Code Playgroud)

我检查了网络标头,Angular调用与jQuery调用之间的主要区别在于,Angular不会将X-Requested-With标头设置为'XMLHttpRequest'吗?

如果我添加它,那么我可以使其正常工作。

 $http.get('/products',{params:{search_terms:query}, responseType:'json', 'headers':{'X-Requested-With':'XMLHttpRequest'}}).success(function(data) {
  // do something
  });
Run Code Online (Sandbox Code Playgroud)

但是对Angular陌生,我不知道这是否是解决此问题的正确方法。每次都必须这样做,感觉有些笨拙。有没有更好的方法,或者这是使用Angular和Rails时的方法?

json ruby-on-rails xmlhttprequest angularjs

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

如何使用理性反应在JSX中有条件地设置HTML属性?

我想呈现一个HTML复选框,其选中状态由数据控制。

给出一个接收item类型的无状态组件{ label: string, checked: bool}

像这样:

let component = ReasonReact.statelessComponent("TodoItem");

let make = (~item, _children) => {
  render: _self => {
     <li> <input type_="checkbox" {/*looking for something like this*/ item.checked ? "checked" : "" /* doesn't compile */}/> {ReasonReact.string(item.label)} </li>
  }
}
Run Code Online (Sandbox Code Playgroud)

如何根据条件将属性的存在添加checkedinput标签item.checked == true

reactjs reason reason-react

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

了解ReasonML中的Js.Promise.resolve(。)点语法

我正在尝试了解文档:https : //reasonml.github.io/docs/en/promise

在用法部分,有:

let myPromise = Js.Promise.make((~resolve, ~reject) => resolve(. 2));
Run Code Online (Sandbox Code Playgroud)

为什么2之前有点?它是什么意思,它是做什么的?

promise reason bucklescript

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

如何使用postgres json字段查询未设置密钥?

我可以选择匹配某个json值的记录where properties->>'foo' = 'bar',但是如果键'foo'尚未设置怎么办?我试过where properties->>'foo' IS NULL但是我收到了一个错误

No operator matches the given name and argument type(s). You might need to add explicit   type casts.
: SELECT "merchants".* FROM "merchants"  WHERE (properties->>'foo' IS NULL)
Run Code Online (Sandbox Code Playgroud)

postgresql json

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