Reputation: 849
Here is my JSON returned from a PHP file (just as a side note, I can use EITHER one):
// JSON Option #1:
[{"field":"title_of_production_row", "required":"1"}{"field":"where_released_row", "required":"1"}{"field":"release_date_row", "required":"1"}{"field":"running_time_row", "required":"1"}{"field":"accepting_award_row", "required":"1"}{"field":"contact_name_row", "required":"1"}{"field":"promocode_row", "required":"0"}{"field":"payment_method_row", "required":"1"}{"field":"tbl_submit", "required":"0"}{"field":"production_company_row", "required":"1"}]
// JSON Option #2: {"title_of_production_row":"1","where_released_row":"1","release_date_row":"1","running_time_row":"1","accepting_award_row":"1","contact_name_row":"1","promocode_row":"0","payment_method_row":"1","tbl_submit":"0","production_company_row":"1"}
I want to loop through each field and required, and alert them. I've tried things like:
$.ajax({
url: './ajax/get_cat_info.php?cid=' + cid,
dataType: "jason",
async: false,
success: function(html) {
alert(html);
$.each(html, function(key, val) {
alert('key: ' + key + ' - val: ' + val);
});
}
});
But this alerts individual characters. Any thoughts?
Upvotes: 1
Views: 505
Reputation: 169068
You made a typo with the datatype
`dataType: "jason"`,
should be
`dataType: "json",`
Upvotes: 1
Reputation: 154504
It looks like you've got two issues:
[{…}, {…}, …]
(noete the commas)."jason"
, not "json"
Upvotes: 2
Reputation: 77986
$.ajax({
url: './ajax/get_cat_info.php?cid=' + cid,
dataType: "json", // Need a correct dataType
async: false,
success: function(html) {
alert(html);
$.each(html, function(key, val) {
alert('key: ' + key + ' - val: ' + val);
});
}
});
JSON Option 1 was invalid: Try this:
[
{
"field": "title_of_production_row",
"required": "1"
},
{
"field": "where_released_row",
"required": "1"
},
{
"field": "release_date_row",
"required": "1"
},
{
"field": "running_time_row",
"required": "1"
},
{
"field": "accepting_award_row",
"required": "1"
},
{
"field": "contact_name_row",
"required": "1"
},
{
"field": "promocode_row",
"required": "0"
},
{
"field": "payment_method_row",
"required": "1"
},
{
"field": "tbl_submit",
"required": "0"
},
{
"field": "production_company_row",
"required": "1"
}
]
For future reference, a great resource is JSONLint.com which allows you to validate your JSON on the fly.
Upvotes: 2