Two part question.
How do I get the post data in the controller the AJAX calls to?
The JSON I’m sending has no identification. For instance:
$.ajax({
type: 'POST',
url: action,
data: JSON.stringify(data),
success: function(data){
alert(data);
},
dataType: 'json'
});
Where data is just [{"name":"variable_name", "value":"12"}]
I’m trying:
public function save()
{
header('Content-Type: application/json');
$json = $this->input->post('data');
echo $json;
}
How do I return something other than JSON?
For example, I was testing this with the following controller:
public function save()
{
echo "test";
}
All I get when the success function is called is null.
Any pointers?
You are going to have to not set a
dataType. When a data type is set to'json'jquery automatically converts the recived data into a js object. Due to you returning just a string (which is not valid json) the json parser doesnt know what to do and just returns null. By removing the data type you can get the raw string and then based on the string’s contents use conditionals to decide whether to parse it as json.