2

I am trying to return json from a module method. Very simple example at this point.

public function mealDates()
{
    echo "{\"foo\" : \"bar\"}";
}

From the template

$.ajax({
    //request dates that are already taken
    type: "POST",
    url: "ACT=79",
    success: function(json){
        console.log(json);
    },
    error: function(e){

    }
});

If I declare dataType: 'json' returns error. If I don't I still am not getting the {"foo" :"bar"} from method. Suggestions on how to return json to template?

Thanks

2

You can use send_ajax_response() which will return JSON and works well for response to ajax requests. For your example you'd do

public function mealDates()
{
    $array = array(
        'foo' => 'bar', 
    );

    return $ee()->output->send_ajax_response($array);  
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.