I have this ajax request which is being called whenever I change an input field:
$(document).ready(function(){
$('#PostTitle').change(function(){
var inputstring=$("#PostTitle").val();
submitData(inputstring);
});
});
function submitData(inputstring){
$.ajax({
type: 'POST',
url: '/myWebsite/posts/set_post_images',
data: inputstring,
dataType: "text",
success:function(data){
$("#selectImage").replaceWith($('#selectImage', $(data)));
},
error:function(){
$('#selectImage').html("You failed misserably.");
},
timeout: 5000
});
}
The above code is in the add.ctp file (a View for the Posts Controller). The called function set_post_images is in the Posts Controller itself and looks like this:
public function set_post_images($url = null) {
if($url!=null){
$this->set('imageArray', $this->getImages($url));
} else {
$this->set('imageArray', array("/img/someImage.png"));
}
if ($this->RequestHandler->isAjax()) {
$this->autoRender = false;
$this->layout = 'ajax';
$this->render('/elements/selectImageElement');
}
return;
}
What I’m trying to do is to get the existing element from the selectImage div replaced by the new element. The problem is, ajax keeps returning the entire contents of the add.ctp file (the file from which the request is called).
What could be the cause of this and how can I fix it?
Edit: Solved
$this->Auth->allow(array(‘set_post_images’)); wasn’t set, so the response was returning the entire page. After fixing that the action could be spoken to.
Also, in the controller, the variable is not accessed via $url but via $this->data[‘url’].
Happy coding!
$this->Auth->allow(array(‘set_post_images’)); wasn’t set, so the response was returning the entire page. After fixing that the action could be spoken to.
Also, in the controller, the variable is not accessed via $url but via $this->data[‘url’].
Happy coding!