0

I want to make a simple rest service request with angular for the following PHP code. For some reasons the request throws an error.

Live code

PHP

<?php
  /* Simple array */
  $json = array("status" => 0, "msg" => "Request method accepted");

  /* Output header */
  header('Content-type: application/json');
  echo json_encode($json);
?>

Angular

var app = angular.module('app',[]);

app.controller('Main', function($scope,$http,$log){
  $http.get({
    method : "GET",
    url : "http://my-host-example/webservice.php",
    headers: {'Content-Type':'application/json'}
  }).then(function(response){
    $log.debug('success',response);
  },function(response){
    $log.debug('error',response);
  });
});
6
  • what error does it throw ? Commented Aug 10, 2016 at 7:22
  • I think you do not need to set header for response Just " echo json_encode($json); exit(); " will be good. Commented Aug 10, 2016 at 7:23
  • @Jigar I placed the live code in jsfiddle, it enters the error function, status 404 Commented Aug 10, 2016 at 7:25
  • @Kashyap tried and didn't work Commented Aug 10, 2016 at 7:27
  • remove this line headers: {'Content-Type':'application/json'}, you are not passing any data with ajax request. Commented Aug 10, 2016 at 7:31

3 Answers 3

2

did you tried this way ?

$http.get("http://webenergized.com/webservice.php").then(function(response){
        $log.debug('success',response);
      }, function(response){
        $log.debug('error',response);
      });
    });

and add in your php

header('Access-Control-Allow-Origin: *');
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I did, for some reasons in jsfiddle throws error 404 not found, but in w3schools editor (with same code) it works....
it's certainly because the php url should be a https:// like fiddle (jsfiddle.net/0d1arfnb)
1

404 ?

  • hosts ok ?

  • permissions to read file ok ?

  • file have run permission?

  • owner is www:www ?

PHP

<?php

die(json_encode(['test' => 'test']));

Angular

$http.get(url); //return promise for service

Cross domain ?

header('Access-Control-Allow-Origin: *');

Comments

0

error status 404 refers file not found at the given url please check the path or call ajax function like this works fine for me.

                $http({
                    url:'ajaxurl',
                    data:{
                        "key":'value',
                         "key":'value'
                    },
                    method:'get'
                }).
                success(function(data){
                    'your code here'

                }).
                error(function(data){
                })

1 Comment

NP bro i didn't use headers config

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.