Receiving Json via post Ajax PHP

0

Good morning!

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

Json Array:

{"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 upload 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 submitting 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?

    
asked by anonymous 21.10.2016 / 13:49

1 answer

1

Try changing your jquery from ajax to 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);
        }
    });

});
    
21.10.2016 / 14:37