1

Hi Guys I am new to angularjs, I am just converting my app from jquery to angular js. But I am facing difficulties with ajax request in angular js. My this code is working fine in jquery.

Angularjs Code:

 links.serverurl = My Another Server Address which is defined another location
 $scope.submit = function () {

 console.log(this.formData);

 $http.get(links.serverUrl).success(function (response) {  
   console.log(response);
 });

JQuery Working Code:

var url = "http://pbc.mydev786.com/";

$.get(url+"?"+data,function(response){
  $("div[data-role='page']").hide();
  $("#showbill").show();
  $(".billresult").html(response);
});

My Php Headers:

 header('Access-Control-Allow-Origin: *');
 header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PATCH, DELETE');  
 header('Access-Control-Allow-Headers: Origin, Content-Type,X-Requested-With');

AngularJs Config :

$myapp.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
        $routeProvider
                .when('/', {templateUrl: "home.html"})
                .when('/electricity', {templateUrl: "electricity.html"})
                .when('/evo', {templateUrl: "evo.html"})
                .when('/ptcl', {templateUrl: "ptcl.html"})
                .when('/suigas', {templateUrl: "suigas.html"})
                .when('/iesco', {templateUrl: "iesco.html"})
                .when('/lesco', {templateUrl: "lesco.html"})
                .when('/fesco', {templateUrl: "fesco.html"})
                .when('/kesc', {templateUrl: "kesc.html"})
                .when('/extra', {templateUrl: "extra.html"})
                .when('/myaccount', {templateUrl: "myaccount.html"});
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }]);

Requested Headers :

Accept  application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  20
Content-Type    text/plain; charset=UTF-8
Host    pbc.mydev786.com
Origin  http://localhost:8383
Referer http://localhost:8383/PakistanCheckBill/
User-Agent  Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0

Response Headers :

Connection  keep-alive
Content-Encoding    gzip
Content-Type    text/html
Date    Tue, 20 Jan 2015 11:47:43 GMT
Server  nginx/1.6.2
Transfer-Encoding   chunked
8
  • stackoverflow.com/a/27950593/967859 Commented Jan 20, 2015 at 10:04
  • didn't worked form me, Question Edited with more code Commented Jan 20, 2015 at 10:52
  • What error to do you get, if any? What is the response from the server file? Commented Jan 20, 2015 at 10:56
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [pbc.mydev786.com. This can be fixed by moving the resource to the same domain or enabling CORS. Commented Jan 20, 2015 at 10:58
  • are you sure what your server responding with needed headers ? Commented Jan 20, 2015 at 11:15

1 Answer 1

2

Only this worked with me not any angularjs trick helped me out

# CORS Headers 
<ifModule mod_headers.c>
Header always set Access-Control-Allow-Origin: "*"
Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "X-Requested-With, content-type"
</ifModule>

i also tried to set these headers with php but that was not working i don't know why.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only solution that worked for me. Modifying the header in my code led to the error: header already sent. btw jquery worked fine, only angular had problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.