Reputation: 205
Here is my JSON function:
function createjson(){
var json='{"jsondata":[[';
$("#kup td").each(function(){
if($(this).html()!="")
{
json+= parseInt($(this).html())+",";
if($(this).index()%5==0 && $(this).index()!=0){
json=json.substring(0,json.length-1);
json+="],["
}
}
});
json=json.substring(0,json.length-3);
json+="]]}";
console.log(json); //-> works fine.
return json;
};
The AJAX part:
$("#button").click(function(){
$.ajax({
type:"POST",
url:"x.php",
data: createjson(),
contentType: "application/json",
dataType: "json",
success: function(result) {
alert("done"); //->works
}
});
});
The PHP part:
<?php
header('Content-type: application/json');
echo "<pre>";
echo $_POST['jsondata'];
echo "</pre>";
?>
So, the "alert" works but when check the response in console, it returns only "<pre></pre>
"
any solution?
Upvotes: 0
Views: 65
Reputation: 16786
The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false
. from jquery API doc
I didn't tested it but should give the intense how to change structure, give it a try:
function createdata(){
var data;
$("#kup td").each(function(){
if($(this).html()!="")
{
data.push( parseInt( $(this).html() ) );
//...
}
});
console.log(data);
return data;
};
$("#button").click(function(){
var data = createdata();
$.ajax({
type:"POST",
url:"x.php",
data: {createjson:data},
contentType: "application/json",
dataType: "json",
success: function(result) {
alert("done"); //->works
}
});
});
Upvotes: 1