0

1.Below is the code which has error for cross domain (CORS). Tried a lot but not able to figure out the mistake please help. The code is in AngularJS the error is "phome1.html:78:7 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."

<script>
        (function() {
          'use strict';

          var app=angular.module('MyApp');

        app.config(['$httpProvider', function($httpProvider) {

                $httpProvider.defaults.useXDomain = true;

                delete $httpProvider.defaults.headers.common['X-Requested-With'];
          }]);

    // Added all header request and response.
        app.controller('DemoCtrl', DemoCtrl);

      function DemoCtrl($scope,$http) {
        var self = this;
        //document.write("hello");
        self.data = null;
        self.selectedItem = null;
        self.searchText = null;

        self.querySearch = function (query) {
          //document.write();
          console.log(escape(query));
          $http.get('http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=a').then(function(result) {
              console.log(result);
            });
        }
      }
    })();
        </script>
AlgoMan
  • 33
  • 8

1 Answers1

0

CORS error can only be solved from server side. There are different reason for this.May be Your server has blocked OPTIONS method for security reasons which is used to get the allowed METHOD (GET, PUT, POST, etc), modern browser first send OPTION request to get the allowed method. So first check this and you can also use "ServletFilter" to add missing "Access-Control-Allow-Origin" add header like below.

      header.add("Access-Control-Allow-Origin","*")

Read this.. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Pandey Amit
  • 657
  • 6
  • 19