-1

first of all to note that I am a very new on AngularJS and I try to create my first app by using the reed.co.uk API, for this reason, please don't shut me .. !! :)

The current state of my application is very basic and consists of the following:

index.html

<!DOCTYPE html>
<html lang="en" ng-app="reed" class="no-js">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Reed Start Up App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap-theme.min.css" />

    <link rel="stylesheet" href="app.css">
    <script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
    <div class="container">
        <div ng-view></div>
    </div>

    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-resource/angular-resource.min.js"></script>
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="app.js"></script>
    <script src="components/services/services.js"></script>
    <script src="viewHome/home.js"></script>
</body>
</html>

app.js

'use strict';

// Declare app level module which depends on views, and components
angular.module('reed', ['ngRoute', 'reed.home', 'reedService'])
    .config(['$routeProvider', function($routeProvider){
            $routeProvider.otherwise(
                {
                    redirectTo : '/'
                }
            );
        }]);

home.js

'use strict';

angular.module('reed.home', ['ngRoute', 'reedService'])
    .config(['$routeProvider', function($routeProvider){
        $routeProvider.when(
            '/', {templateUrl : 'viewHome/home.html', controller : 'HomeCtrl'}
        );
    }])
    .controller('HomeCtrl',['$scope', 'Jobs', function($scope, jobs){
        $scope.performSearch = function()
        {
            console.log(
                jobs.query(
                    {
                        keywords : $scope.jobTitle
                    },
                    {
                        method : 'GET',
                        headers : {
                            username : '9f0ebf61-8795-49d7-a1d7-123456789123'
                        }
                    }
                )
            );
        };}
    ]);

home.html

<div class="row">
    <div class="col-sm-12">
        <form role="form">
            <div class="form-group">
                <label form="job-title">
                    Job Title
                </label>
                <input ng-model="jobTitle" id="job-title" placeholder="Please enter your desired job title" class="form-control" type="text" />
            </div>
            <div>
                <button type="submit" class="btn btn-default" ng-click="performSearch()">Submit</button>
            </div>
        </form>
    </div>
</div>

services.js

'use strict';

var jobsServices = angular.module('reedService', ['ngResource']);

jobsServices.factory('Jobs', ['$resource', '$http', function($resource, $http){
        return $resource(
            'http://www.reed.co.uk/api/1.0/search',
            {},
            {
                query : {
                    method : 'GET',
                    headers : {
                        username : '9f0ebf61-8795-49d7-a1d7-123456789123'
                    }
                }
            }
        )
}]);

The Reed.co.uk API documentation says the following:

Important:

You will need to include your api key for all requests in a basic authentication http header as the username, leaving the password empty.

Based on the above message I try to set my API key as username on my request headers. When I try the above approach with API Request plugins on my Chrome, the request is ok. I get normally whatever I requesting with no issue.

In my AngularJS app, the problem is that while I send a GET request, the AngularJS changing my request method to OPTIONS when I set my headers.

If I remove the headers, then the request performed with GET, but ofcourse I get Unothorized request error from the server.

Can somebody help me please ?

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166

1 Answers1

1

Question is not angular specific. Answer here is pretty good:

The OPTIONS request what you see is the preflight request, you can read about that here:

There are two solutions to solve the problem (as mentioned above):

  • implement the response for the OPTIONS request with the corresponding Access-Control-* headers
  • use a JSONP request instead of simple JSON
Community
  • 1
  • 1
tunguski
  • 763
  • 7
  • 12