0

I've been having this problem for a while and tried many solutions which haven't worked. I am trying to use $http.post to send json data to a php script named processCustomers.php which is part of my API

The post request is

angular.extend(obj, {id: id});
  $http({
    method: 'POST',
    url: '../API/processCustomers.php',
    headers: { 'Content-Type': 'application/json' },
    dataType: 'json',
    data: obj
   }).success(function (data) {
    console.log(data);
  });
};

and the processCustomers is

$newCustomers = filter_has_var(INPUT_POST, 'newCustomer');
if ($newCustomers) {#request to add new customer

    $exceptionalFields = ['customerID']; //

    $customersJSON = json_decode(filter_input(INPUT_POST, "newCustomer"));



    if (json_last_error() > 0) {
        echo DotCom::systemFeedBack("Sorry customer not created unknown error", "data sent is not a json object", 303);
        return;
    }



    foreach ($customersJSON as $key => $customer) {
        $parameterTypes = ""; # order determined by the sql returned by validateInputData()


    $entries = $func->validateInputData($tableMetaData,$customer, $parameterTypes, $exceptionalFields);


    if (!is_array($entries)) {
        echo $entries;
        return;
    }

     $results = $customers->createCustomer($entries, $parameterTypes, $link,$status);

When I send the request, the console in the browser says the response is text/html instead of application/json.I looked through many tutorials and examples and I also have tried the folowings: removing the headers and datatype - no effect wrapping the obj in JSON.stringify - results in header changing to application/x-www-form-urlencoded

2
  • what is the issue...receiving in php or receiving in client? Commented May 14, 2015 at 5:13
  • I don't know much about PHP, but you said your response that you are getting back is of type text\html so did you try setting up contenttype in php like header('Content-Type: application/json'); ? Commented May 14, 2015 at 5:14

1 Answer 1

1

You need the following in your php file:

$json = file_get_contents('php://input');
$obj = json_decode($json);

$_POST will be empty when Content-Type: application/json is passed in headers.

Learned few days back from this questions

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

Comments

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.