I have created a web service using php and when I access from a rest client I get the proper JSON response. I am calling the method from AJAX call integrated in a Drupal site. the service response is HTML hence it is going to error block.
PHP CODE:
$api_response = array('result' => 1, 'message' => 'Successfully user added!');
header("Content-Type: application/json; charset=utf-8");
$json_response = json_encode($api_response);
echo $json_response;
JS CODE:
$.ajax({
url:'http://localhost:8888/testService.php',
type: 'GET',
dataType:'json',
success: function(data) {
console.log("SUCCESS DATA");
},
error: function(error) {
alert("ERROR OCCURED 123");
}
});
</div>
(Posted on behalf of the OP).
The PHP file was present in the same location that of the Drupal instance. Drupal site has a page with name service hence it was returning the HTML template. I moved the service file outside of the Drupal instance and it worked.