Receive Json via post Ajax PHP

Asked

Viewed 641 times

0

Good morning!

I have this code that sends an array containing data in json format:

Array Json:

{"cliente":[{"Code":"1","Name":"A"},{"Code":"2","Name":"B"}]}

Ajax:

$('#btn-sinc').click(function(){

            $.ajax({
                  type: "POST",
                  url: 'http://localhost/appread/Post.php',
                  data: "cliente="+ClienteStorage,
                  crossDomain: true,
                  contentType: "application/json; charset=utf-8",
                  dataType: 'jsonp',
                  success: function (responseData, textStatus, jqXHR){
                      console.log(responseData);
                  },
                  error: function (responseData, textStatus, errorThrown) 
                  {
                      console.warn(responseData, textStatus, errorThrown);
                        alert('Falha no envio - ' + textStatus);
                  }
            });

        });

This Ajax in Project 1 folder and sends to PHP Project 2

PHP:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');

echo json_encode(array($_POST['cliente'], true));

When sending returns this error:

Uncaught SyntaxError: Unexpected token
    Post.php?callback=jQuery1520194…_1477050103032&cliente={"cliente":[{"Code":"1","Name":"…:1 


Object {readyState: 4, status: 200, statusText: "parsererror"}abort: (a)complete: ()done: ()error: ()fail: ()getAllResponseHeaders: ()getResponseHeader: (a)isRejected: ()isResolved: ()overrideMimeType: (a)promise: (a)readyState: 4setRequestHeader: (a,b)status: 200statusCode: (a)statusText: "parsererror"success: ()then: (a,c)__proto__: Object "parsererror" "jQuery15201942096755848719_1477050103032 was not called"

Does anyone have any idea what I can do?

  • Hello friend, in ajax, in dataType, is "jsonp" even? not "json"?

  • @Gabrielsantos if I put json it returns null also it falls inside the error: function (responseData, textStatus, errorThrown)

  • And if you change this line: date: "client="+Clientestorage, For this: date: Clientestorage, JSON itself is already saying that the name is client

1 answer

1


Try changing your ajax Jquery for this.

$('#btn-sinc').click(function(){

    $.ajax({
        type: "POST",
        url: 'http://localhost/appread/Post.php',
        data: ClienteStorage, //***Esta Linha
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        dataType: 'json', //***Esta Linha
        success: function (responseData, textStatus, jqXHR){
            console.log(responseData);
        },
        error: function (responseData, textStatus, errorThrown) {
            console.warn(responseData, textStatus, errorThrown);
            alert('Falha no envio - ' + textStatus);
        }
    });

});

Browser other questions tagged

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