So I am trying to pass what I think is a json array through an ajax post, but I am having no luck. If I change the dataType to "text" and just add some text as my data, it works fine, which tells me its my data format.
I am getting a 'null' when dumping data on my php page.
This is what my data looks like before it is posted:
[{"name":"La Sal, UT","type":"city"},{"name":"Utah, US","type":"state"},{"name":"United States","type":"country"}]
Here is the code that generates that data:
var groups=[];
if(town && stateShort){
groups.push({name: town+", "+stateShort,
type:"city"
});
}
if(neighborhood && stateLong){
groups.push({name: neighborhood+", "+stateShort,
type:"neighborhood"
});
}
if(stateLong && countryShort){
groups.push({name:stateLong+", "+countryShort,
type:"state"
});
}
if(countryLong){
groups.push({name:countryLong,
type:"country"
});
}
groups = JSON.stringify(groups);
console.log(groups);
$(document).ready(function(){
$.ajax({
type: "POST",
url: "check_groups.php",
data: groups,
dataType: 'json',
success: function (data) {
alert("success");
},
error: function(xhr) {
if(xhr.status == 422) {
alertValidationErrors(parseErrors(xhr));
} else {
alert('An error occurred while processing the request.');
}
}
});
});
I suppose that it makes sense that that data doesn't post, as it does not look like any json object I've ever posted, but nonetheless, I sort of expected json_stringify() to be a sort-of catch-all solution.
So my question is, what should I change to properly format this for json?
Sincere thanks for any help. It is greatly appreciated.