1

I have a Scrapyd server running and trying to schedule a job.

When i try below using CURL it is working fin e

curl http://XXXXX:6800/schedule.json -d project=stackoverflow -d spider=careers.stackoverflow.com -d setting=DOWNLOAD_DELAY=2 -d arg1=val1

After that i have done a small code UI in angular to have a GUI for this,

I have done a AJAX request to do the above.

 var baseurl = GENERAL_CONFIG.WebApi_Base_URL[$scope.server];
            var URI = baseurl +"schedule.json";  //http://XXXXX:6800/schedule.json
           var headers = {'content-type': 'application/x-www-form-urlencoded'}

            console.log(URI)

             $http.post( URI,data =  $scope.Filters,  headers).success(function (data, status) {


                console.log(data)


            }).error(function (data, status) {
                console.log(status);

                alert("AJAX failed!");
            });

but i am getting No 'Access-Control-Allow-Origin' header is present on the requested resource. error.

Can any one help me how to resolve this ?

And why it is working in CURL but not in my AJAX.

Thanks,

backtrack
  • 7,996
  • 5
  • 52
  • 99

1 Answers1

-1

This is because of browser protection called Same-origin policy. It prevents ajax requests across a different combination of scheme, hostname, and port number. Curl has no such protection.

In order to prevent it you will either have to put both the api and client app on the same domain and port or add the CORS header 'Access-Control-Allow-Origin' to the server.

One other option is to use JSONP. This may be suitable in this case to just get json data. It's not suitable for rest apis. In angular use $http.jsonp for this

Wayne Ellery
  • 7,888
  • 1
  • 28
  • 45
  • I do not have control over the Scrapyd (Twisted server) Is there any thing that i can do in the client level – backtrack Jan 10 '15 at 10:59
  • You could use [JSONP](http://en.wikipedia.org/wiki/JSONP) but it's probably not suitable because it really only allows for just get requests. It's not good for rest apis. Why are you posting to a .json extension anyway. I think you want to perform a get instead so JSONP could work – Wayne Ellery Jan 10 '15 at 11:03
  • https://scrapyd.readthedocs.org/en/latest/api.html#schedule-json .. I am using Scrapyd so there is .json in my request url – backtrack Jan 12 '15 at 09:26