Abhilash
Abhilash

Reputation: 2953

rails get request not working properly using angular

I am trying to implement a search page using angular in my rails app. If i go to http://0.0.0.0:3000/search.json I am successfully getting a json response in the browser aswell. so the rails part is working fine.

But in chrome if I press search button am getting the following error

XMLHttpRequest cannot load %30.0.0.0:3000/search. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource

and in firefox am getting not found 404 error.

app.js

app.controller('searchCtrl', function ($scope, $http) {


    $scope.getrooms = function(){
        $http({
            url: "0.0.0.0:3000/search",
            method: "GET"
        }).success(function(response){
            $scope.rooms = response;
        });
    };
    });

pages_controller.rb

  def search
    @rooms_address = Room.all
    respond_to do |format|
      format.html
      format.json { render :json => @rooms_address }
    end
  end

search.html.erb

  <input type="text" ng-model="searchLocation">

   <button ng-click="getrooms()">Search</button>

Could someone tell me what am missing here?

Upvotes: 1

Views: 80

Answers (2)

rPat
rPat

Reputation: 375

        url: "0.0.0.0:3000/search"

Have you tried using "http://0.0.0.0:3000/search", or "/search"? The docs do not write the url the way you did. I'd recommend just using "/search".

Edit: Also, have you opened up your console and looked at your network requests? Look at the request header, and make sure it is the correct url. In the future, that should be your first course of action when debugging.

Upvotes: 1

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23522

Are you loading your angular files from file://?

Browsers can't make HTTP Requests that way. Host your static files on your server and all should be well.

Upvotes: 0

Related Questions