Json post for PHP

Asked

Viewed 133 times

-1

Hello,

I need to get the data that are being passed via Ajax POST. But on the PHP side I’m not able to get past parameters.

My code Jquery

onSave: function(data, name) {
        pixie.http().post('images/gravar_imagem.php', [{"name": name, "data": data}]).subscribe(function(response) {
            console.log(response);
        });
    },

PHP

<?php 
     $postdata = file_get_contents("php://input");
     $request = json_decode($postdata);
     $name = $request->name;
     $data = $request->data;
     
     echo $name; 
?>

appears the following error:

error: Syntaxerror: Unexpected token < in JSON at position 0 at JSON.parse () at Xmlhttprequest. u

text: "
Notice: Trying to get Property 'name' of non-object in C: xampp htdocs Pixie images gravar_imagem.php online 17

  • The error is inside the server, when there is an error on the server, the answer comes with html tags '<' when there is a php error will add tag with the error message. Therefore, your json contains html tags and becomes invalid due to unexpected tags.

  • You send json pro php so you shouldn’t give a json_decode instead of Encode?

  • @fajuchem error my I wrote wrong.. edited the question.

  • @Tfm will check if you really have html tags on my Jason.

  • What returns you console.log(Answer)?

  • He doesn’t get to log in to the console.log(responds)

Show 1 more comment

1 answer

0


Posting to show how I resolved the doubt I had:

In the method Onsave

onSave: function(data, name) {
        /*pixie.http().post('images/gravar_imagem.php', [{"name": name, "data": data}]).subscribe(function(response) {
            console.log(response);
  });*/

  $.ajax({
     type: 'POST',
     url: 'images/gravar_imagem.php',
     data: { data: data , name: name},
   }).success(function(response) {
      alert(response);
   });
  },

Browser other questions tagged

You are not signed in. Login or sign up in order to post.