Javascript: ¿Enviar un objeto JSON con Ajax?
Es esto posible?
xmlHttp.send({
"test" : "1",
"test2" : "2",
});
Tal vez con: un encabezado con content type
: application/json
?:
xmlHttp.setRequestHeader('Content-Type', 'application/json')
De lo contrario puedo usar:
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
Y luego JSON.stringify
el objeto JSON y enviarlo en un parámetro, pero sería genial enviarlo de esta manera si es posible.
116
3 answers
Con jQuery:
$.post("test.php", { json_string:JSON.stringify({name:"John", time:"2pm"}) });
Sin jQuery:
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify({name:"John Rambo", time:"2pm"}));
267
Author: Nathan Romano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-22 18:50:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-22 18:50:35
Si no está utilizando jQuery, asegúrese de:
var json_upload = "json_name=" + JSON.stringify({name:"John Rambo", time:"2pm"});
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", "/file.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(json_upload);
Y para el extremo receptor de php:
$_POST['json_name']
29
Author: shantanu chandra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-18 02:56:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-18 02:56:10
Añadiendo Json.stringfy
alrededor del json se solucionó el problema
0
Author: user3310115,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 23:02:54
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 23:02:54