Reputation: 11605
I'm using the Twitter Bootstrap as the base framework for my web application.
I'm trying to use JSON to pass data back from my PHP script to the client, using this piece of javascript:
$(function($) {
$('form[data-async]').live('submit', function(event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
dataType: 'json',
success: function(data, status) {
$.getJSON(url, function(data) {
var response = $.parseJSON(data);
$target.html(response.html)
});
}
});
event.preventDefault();
});
});
However, what is being written to the model is JSON data and not the HTML I'm expecting.
Server-side code:
$ContentBits['content'] = $this->content;
$html = ContentFactory::capture_output(ROOT . DS . 'library' . DS . 'cbd' . DS . 'templates' . DS . 'ajaxAlert.php');
$jsonArray = array(
'html' => $html,
'resultCode' => 1
);
$json = json_encode($jsonArray);
header('Content-Type: application/json');
echo $json;
Upvotes: 0
Views: 84
Reputation: 1038730
The $.getJSON
method already parses the result from the server as JSON, so you don't need to call $.parseJSON
once again. The data
parameter that is passed to your callback will already represent a JSON object that you could use directly:
$.getJSON(url, function(data) {
$target.html(data.html);
});
Upvotes: 2