JSON cut when sending by post with AJAX

Asked

Viewed 589 times

4

I’m using Angular to register in the database, I take the data of my inputs put into an object and turn into a JSON and send by post, but it turns out that my JSON is cut and I don’t have a clue why.

 var meuJson = angular.toJson(meuObjeto);
 $http({
   method: 'POST',
   url: 'http://url/arquivo.php',
   data: {
     'data': meuJson
   }
 })
 .success(function (data){
    console.log(data);
  })

My JSON is popping up like this.

[
  {
    "uuid":"56456456456456456456465456"
  },
  {
    "store_name":"",
    "store_email":"",
    "store_facebook":"",
    "contact_name":"Juca",
    "contact_email":"[email protected]",
    "contact_facebook":"http://localho

I gave a console.log() with Json when I pick him up and show him, then I show the answer, see the image that gets better to understand:

Clique aqui para ver a imagem

  • He’s cutting before uploading or in the.php file?

  • Do you store JSON in the database and retrieve it? Or do you convert a query in the database into a JSON?

  • @Alessandrogomes when I give the console.log on my machine before sending it is correct. So on arquivo.php i give a var_dump($_POST) and appears already cut.

  • @Diegolopeslima I saved it on site Recovery and shipping Storage.

  • Can I try to give more details please?

  • @Diegolopeslima, a json is sent via POST, to a.php file, this php file does nothing but give a var_dump($_POST) (we are debugging), appears the string that is being sent, only cut, as described in the question.

  • Are you using any CMS, framework or template engine? (Wordpress for example)

  • @Diegolopeslima, no, pure php.

  • Have you tried with jQuery?

  • You can post the respective PHP snippet you receive?

  • @Khronna, you can put the whole JSON, or at least the line following the part where it is cut? I have a suspicion.

  • @Sergio I’m participating in this project too, so I put an image of the console with the entire array and then how it comes back.

  • Follow in Fiddler2 and see how the request and response are behaving

Show 8 more comments

1 answer

2

I’m not allowed to comment yet, so I’ll post an alternative anyway: well, I’ve had similar problems with cut transfers, and one of the main problems in this case is character encoding. For some reason I do not know, when the encoding of the sent text is different from the encoding expected by the server in certain cases it cuts the string from a character that it cannot interpret - I think.

To check the encoding of the text being sent use the method mb_detect_encoding. If the encoding of the received JSON is different from the expected try forcing the conversion by PHP, or by setting the charset of the page that is sending JSON.

Converting with PHP:

$charset = mb_detect_encoding($_POST['data']);
$json = mb_convert_encoding($_POST['data'], "UTF-8", $encoding);

Converting with the HTML: http://www.w3c.br/cursos/html5/conteudo/capitulo3.html

Browser other questions tagged

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