简体   繁体   中英

Retrieving JSONP in PHP not working

I'm pulling my hair off with this issue: I'm sending some data from a form from one domain to another using JSONP, It works marvelous on my localhost, but is just not working on the server. I'm using CodeIgniter 2.X for the backend. Here's some code:

 var telefono = $('#telefono').val();
        var email = $('#email').val();
        var mensaje = $('#mensaje').val();
        $.ajax({
            url: 'http://www.example.com/site/admin/index.php/controller/save',
            type: 'POST',
            dataType: 'jsonp',
            data: JSON.stringify({nombre:nombre,telefono:telefono,email:email,mensaje:mensaje,idPropiedad:46}),
            cache: false,
            processData: false,
            crossDomain: true
        })

On CodeIgniter:

$info = json_decode(file_get_contents("php://input"));
var_dump($info);

That's how I retrieve the data, It works like a charm on my localhost. Yet I get NULL on this server. I also tried dumping the $_POST array, nothing there.

This is what I get dumping the $_GET array:

 array(3) {
  ["callback"]=>
  string(42) "jQuery1111037374000856652856_1438207931357"
  ["{\"nombre\":\"fasf\",\"telefono\":\"asfasfasfas\",\"email\":\"asfasfas\",\"mensaje\":\"fasfasfa\",\"idPropiedad\":46}"]=>
  string(0) ""
  ["_"]=>
  string(13) "1438207931360"
}

I also tried sending the data with FormData() from the frontend, nothing.

This is what's being sent:

Request URL:http://www.example.cl/site/admin/index.php/controller/save?callback=jQuery1111037374000856652856_1438207931357&{%22nombre%22:%22fasf%22,%22telefono%22:%22asfasfasfas%22,%22email%22:%22asfasfas%22,%22mensaje%22:%22fasfasfa%22,%22idPropiedad%22:46}&_=1438207931359
Request Method:GET
Status Code:200 OK

Any ideas? Thanks in advance.

Using POST with jsonp is a contradiction. As you can see, GET is being used. The rest of your data is unavailable (well, difficult to access at least) because you stringified it. If you just pass the unstringified object, jQuery will turn your object into key/value pairs in the URL.

var telefono = $('#telefono').val();
var email = $('#email').val();
var mensaje = $('#mensaje').val();
$.ajax({
    url: 'http://www.example.com/site/admin/index.php/controller/save',
    dataType: 'jsonp',
    data: {
       nombre: nombre,
       telefono: telefono,
       email: email,
       mensaje: mensaje,
       idPropiedad: 46
    }
});

Then, you can access the data via:

$nombre = $_GET['nombre'];
$telefono = $_GET['telefono'];
... etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM