Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails post后Ruby on rails路由错误_Ruby On Rails_Json_Ionic3 - Fatal编程技术网

Ruby on rails post后Ruby on rails路由错误

Ruby on rails post后Ruby on rails路由错误,ruby-on-rails,json,ionic3,Ruby On Rails,Json,Ionic3,我在尝试从Ruby返回json时遇到了一个问题 这是我的控制器(parts\u controller) 我的路由文件: Rails.application.routes.draw do root to: "home#index" resources :parts end 这是我在爱奥尼亚的发帖方法 postData() { let headers = new Headers(); headers.append('Content-Type', 'application/j

我在尝试从Ruby返回json时遇到了一个问题

这是我的控制器(parts\u controller

我的路由文件:

Rails.application.routes.draw do

  root to: "home#index"
  resources :parts


end
这是我在爱奥尼亚的发帖方法

postData()
{

  let headers = new Headers();
  headers.append('Content-Type', 'application/json');

    let body = {
   id: 1
  };

    this.http.post('http://localhost:3000/parts/1', JSON.stringify(body), {headers: headers})
    .map(res => res.json())
    .subscribe(data =>{
        console.log(data);
   });          
}
如果我手动输入,它会返回JSON,但当我通过post获取它时,它会抛出:post 404(未找到)

我还有show和index方法的视图,谢谢您的时间。

如何修复它 为什么? 因为这个节目应该有GET not POST方法。POST用于向后端发送(POST)数据,而不是获取数据

如果您使用rake路由或rails路由,您将找到正确的路径以及可以用于每个操作的方法。

如何修复它 为什么? 因为这个节目应该有GET not POST方法。POST用于向后端发送(POST)数据,而不是获取数据


如果您使用rake路由或rails路由,您将找到正确的路径以及可以用于每个操作的方法。

我为所有http请求创建了一个父类

api.service.ts

在每个请求之前,我都写了一个课程

http.token.interceptor.ts


您需要为服务器端安装COR机架

Gemfile.rb

你必须加入到

application.rb


我为所有http请求创建了父类

api.service.ts

在每个请求之前,我都写了一个课程

http.token.interceptor.ts


您需要为服务器端安装COR机架

Gemfile.rb

你必须加入到

application.rb


我认为您正在向get方法中定义的url发送post请求。这就是为什么会出现此错误。我认为您正在向get方法中定义的url发送post请求。这就是为什么您会出现此错误。非常感谢,这是错误,祝您愉快。欢迎:)请将其标记为正确,以便其他人知道它对您有效。非常感谢,这是错误,祝您愉快。欢迎:)请将其标记为正确,以便其他人知道它对您有效。
postData()
{

  let headers = new Headers();
  headers.append('Content-Type', 'application/json');

    let body = {
   id: 1
  };

    this.http.post('http://localhost:3000/parts/1', JSON.stringify(body), {headers: headers})
    .map(res => res.json())
    .subscribe(data =>{
        console.log(data);
   });          
}
this.http.get('http://localhost:3000/parts/1', {headers: headers})
    .map(res => res.json())
    .subscribe(data =>{
        console.log(data);
   }); 
import {HttpClient, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {ENV} from '@app/env';
import {Observable} from "rxjs/Observable";

@Injectable()
export class Api {
  constructor(public http: HttpClient) {}

  post(endpoint: string, body: any) {
    return this.http.post(ENV.SERVER_URL + '/' + endpoint, body);
  }
}
export class TokenInterceptor implements HttpInterceptor {
  constructor(public auth: AuthService) {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    request = request.clone({
      setHeaders: {
        Accept: 'application/xxx.v1+json',
        Authorization: "bearer " + this.auth.getToken()
      }
    });
    return next.handle(request);
  }
}
...
constructor(public api: Api) {}

  getParts(): Observable<Part[]>{
    return this.api.get("parts")
      .map(res => {
        if(res) {
          return res.map(item => {
            return new Part(item);
          });
        }
      });
  }
...
this.partService.getParts().subscribe(parts => {
   ..
   });
},(error: Response) => {
  return Observable.throw(error)
});
gem 'rack-cors'
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: %i[get post options patch delete]
      end
    end