且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拒绝从执行脚本“*”,因为它的MIME类型(“应用/ JSON')不是可执行文件,以及严格的MIME类型检查已启用

更新时间:2022-04-27 22:17:11

哎,所以我相信你的问题是,你的导轨控制器返回JSON和NOT JSONP。控制器具有明确指定的回调函数,可以由请求参数来指定。

Hey so I believe your problem is that your rails controller is returning JSON and NOT JSONP. Your controller has to explicitly specify a callback function, which can be specified by the request params.

请参阅处理JSONP在轨道3控制器用于返回JSONP的例子从Rails控制器

See Handling jsonp in rails 3 controller for an example of returning JSONP from a rails controller

所以,你的导轨code会是什么样子(哎呀我的Rails是非常非常生疏...):

So your rails code would look like (argh my rails is very very rusty...):

class Api::V1::PhonesController < ApplicationController
  def index
    if params[:callback]
      format.js { render :json => {:phones => Phone.all.to_json}, :callback => params[:callback] }
    else
      format.json { render json: {:phones => Phone.all.to_json}}
    end
end

然后对角边,这个答案应该帮助你:
parsing在angular.js

我想那么你的角度看起来像:

And I think your angular would then look like:

var appServices = angular.module('appServices', []);

phoneCatApp.factory('appServices', ['$http', '$q', function($http, $q){
  var url = "http://localhost:3000/api/v1/";

  //get all phones
  this.getPhones = function(){
    var defered = $q.defer();
    var listApi = url + "phones?callback=JSON_CALLBACK";

    $http.jsonp(listApi).then(function(results){
      defered.resolve(results);
    }, function(error){
      defered.reject(error);
    });
    return defered.promise;
    }
  return this;
}]);