I'm trying to create a favorite/star/heart button (like in Gmail) for each item on a list of elements in CakePHP 2.2.
I've got a working controller and a JS toggle function but my problem is that the AJAX parts of my scripts aren't actually happening; instead the page redirects to itself (as part of the fallback for browsers with JavaScript off) and the reloads. This is what I'm trying to avoid so the users don't loose their position on the page.
Here's my CakePHP controller that Un-Favorites an item:
class MarksController extends AppController {
public $components = array('RequestHandler');
public function unfav($id = null) {
// set default class & message for setFlash
$class = 'flash_bad';
$msg = 'Invalid List Id';
if ( validation($id) ) {
//load this mark's data
$this->request->data = $this->Mark->read(null, $id);
$this->request->data['Mark']['favorite'] = '0';
if ($this->Mark->save($this->request->data)) {
$class = 'message';
$msg = 'The WatchMark has removed from favorites.';
} else {
$msg = 'This WatchMark could not be saved. Please, try again.';
}
$this->autoRender= false;
$this->autoRender = $this->layout = false;
// output JSON on AJAX request
if($this->request->is('ajax')) {
echo json_encode(array('success'=>($class=='flash_bad') ? FALSE : TRUE,'msg'=>"{$msg}"));
exit;
}
// set flash message & redirect
$this->Session->setFlash($msg,'default',array('class'=>$class));
$this->redirect($this->referer());
} else {
//trying to edit a mark that doesn't belong to this user:
throw new NotFoundException(__('Invalid mark'));
} // end ownedby check
} //end markfav method
I have another class that's the inverse function called markfav (combining them into a single toggle function is a task for another day).
Then, on the JQuery & JavaScript side:
$(document).ready(function() {
// class exists
if($('.fav_toggler').length) {
// add click handler
$('.fav_toggler').click(function(e){
$('#flashMessage').fadeOut();
if($(this).hasClass("heart")) {
$.post('/marks/markfav/' + $(this).attr('target_id'), "ajax=1", function(response) {
if(response.success == true) {
$(this).addClass("heart").removeClass("hearttoggle");
}
},'json');
}
else {
$.post('/marks/unfav/' + $(this).attr('target_id'), "ajax=1", function(response) {
if(response.success == true) {
$(this).addClass("heart").removeClass("hearttoggle");
}
},'json');
}
e.preventDefault();
});
return false;
}
});
I've tested by throwing alerts into my JavaScript and I think that it's executing correctly (it gets to the right part of my code and changes the class between heart & hearttoggle.
I also know that it's skipping the if($this->request->is('ajax')) block in my PHP for some reason (though I have an AJAX delete function that hits a similar conditional just fine). I've tested removing the if($this->request->is('ajax')) condition from the PHP block to force it to execute the JSON_encode bit and I just end up with a page containing the JSON encoded array and nothing else.
How do I make this work correctly using AJAX without a page reload?
EDIT: Headers from call to /marks/unfav/624
Request URL:http://towatchlist.com/marks/unfav/624
Request Method:GET
Status Code:200 OK
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:CAKEPHP=blah
Host:towatchlist.com
Referer:http://towatchlist.com/marks
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1255.0 Safari/537.8
Response Headers
Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:text/html
Date:Sun, 09 Sep 2012 20:55:18 GMT
Keep-Alive:timeout=5, max=94
Server:Apache/2.2.22 (Ubuntu)
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.10-1ubuntu3.2
For some reason X-Requested-With is not presented. It should show this in your request header:
X-Requested-With: XMLHttpRequest
Here are some questions that may help:
Jquery, No X-Requested-With=XMLHttpRequest in ajax request header?
X-Requested-With header not set in jquery ajaxForm plugin
Is the AJAX request that is working being called the same way that this one?
Related
I'm working with Slim Framework and I would like to redirect the user to the login page if the user has lost his session but I'm always getting a SyntaxError : Unexpected token < at position 0.
My session validation code in php is this:
private function _validaSessao() {
$user = $this->userData['IdUser'];
if(null === $user || trim($user) == '') {
header("Location: http://192.168.0.9/", true, 301);
die();
}
}
I've tried that and all the following:
header('refresh:5;url=http://192.168.0.9/');
echo '<script>window.location.href = "http://192.168.0.9/";</script>';
return('<script>window.location.href = "http://192.168.0.9/";</script>');
echo json_encode('<meta HTTP-EQUIV="REFRESH" content="0; url=http://192.168.0.9/">');
I've tried them all and I'm always getting
200 ---- SyntaxError: Unexpected token < in JSON at position 0
The only piece of code that worked for me was:
echo json_encode(array(
'SemSessao' => true
));
But the above code makes me checking on every single call on JavaScript and I would like a solution that PHP will redirect me. This way I wouldn't need to keep checking on every single JS call (which are a lot) and each time a php object was instanciated it would check for session and redirect the user without the use of JS.
Update 1 - Include JS code (lovely downvotes everywhere :D)
getDadosPlaneamento: function() {
var req = {Rota: '/planeamento/getDados/AUTO'};
var dfd = $.Deferred();
$.when(App.gajax(req)).done(function(d) {
On.Planeamentos = d.Planeamentos;
dfd.resolve();
});
return dfd.promise();
},
The above code is what refers to my php route and then:
$onapp->get('/planeamento/getDados/:tipo/', function($tipo) {
if ($tipo == 'AUTO') {
$P = new MongoApi\Planeamento();
$ret = array(
$P->getAllMongo();
);
}
echo json_encode($ret);
});
And when I do $P = new MongoApi\Planeamento(); I check if the user has a valid session on the constructor using _validaSessao();
The server cannot redirect a client from an AJAX call. The AJAX call is a background HTTP request. Whether that HTTP requests gets redirected or not is irrelevant to the browser. The browser will return the request response to the AJAX client, and if that response is "your request has been redirected" then that's that. Again, a redirect doesn't redirect "the browser", it redirects the HTTP request. Or more precisely speaking, it tells the HTTP client that it should retry its request somewhere else; nothing more.
If your AJAX requests can fail due to a session timeout and whenever that happens you want to present the user with a login page, you will have to do that client side. In order to not repeat that same code every time, you make a function/object/service out of that. E.g. something along the lines of:
function makeAJAXRequest(url, data) {
return fetch(url)
.then(response => {
if (response.status == 403) {
window.location = '/login';
throw new Error('Forbidden');
} else {
return response;
}
});
}
Here the server is expected to respond with a 403 Forbidden status code for unauthorised requests. If you make all your AJAX requests through this function, it will automatically handle that case by redirecting to the login page.
Remeber that header() must be called before any output is generated. you can use ob_start() and op_end_flush() to avoid output previous to your header.
ob_start ();
header ("Location: http://192.168.0.9/", true, 301);
ob_end_flush ();
I can't figure out how to create a correct server response after calling Backbone.Model.save().
Consider myModel to be an initialized Backbone Model instance.
myModel.set('foo', '123');
myModel.save().then( function() {
console.log('saved foo');
}, function() {
console.log('error');
} );
The console output is 'error' allways.
The new value for 'foo' is set on the server though and after reloading the app the view representing myModel.get('foo') will show the expected value ('123').
However the promise never evaluates to the success function.
As my server is very simple I am almost certain that I am doing something wrong there - which is this
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
if( isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) ) {
if( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] == 'PUT' ) {
// ... doing stuff to update data on server
http_response_code(200);
echo '';
}
}
}
This is the received response header
Content-Length 0
Content-Type text/html; charset=UTF-8
Server PhpStorm 9.0.1
X-Powered-By PHP/5.6.12
I very much appreciate every hint on what I am missing here.
header() is used to set HTTP-headers,
So add the line of code
header("Content-type: application/json");
$rtn = array("status", "200");
http_response_code(200);
echo json_encode($rtn);
I have an issue very similar to this one: jquery json function returning null.
I have followed the above contained advice, however, and am still seeing null as a result.
Here is my code:
JS:
Gallery.prototype.getImages = function(opt){
var self = this;
var baseurl = 'http://local.gallery.dev'
$.ajax({
url: baseurl+='/controllers/ajax.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: {action : opt},
dataType: 'JSON',
success: function(data){
//self.setImages(data);
console.log(data)
},
error: function(){
console.log('NOPE');
}
});
}
PHP:
class ajax_controller {
function __construct(){
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'Newborn' : $this->Newborn();
break;
}
}
}
/*
* Process Newborn Gallery request.
*/
public function Newborn(){
header('Content-type: application/json');
echo json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
}
}
The console/debugger/network panel are all saying that I am talking to the ajax controller correctly, however, the data of the success method only returns null.
I am fairly novice to PHP, any suggestions greatly appreciated.
Thanks, Ken
UPDATE
My call is still returning null so I thought i'd paste my headers here.
Request URL:http://local.sunnyrose.dev/controllers/ajax.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:14
Content-Type:application/json; charset=UTF-8
Host:local.sunnyrose.dev
Origin:http://local.sunnyrose.dev
Referer:http://local.sunnyrose.dev/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML,
like Gecko) Chrome/17.0.963.56 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
action=Newborn
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json
Date:Mon, 05 Mar 2012 17:49:53 GMT
Keep-Alive:timeout=5, max=92
Server:Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4
Perl/v5.10.1
X-Powered-By:PHP/5.3.1
echo at the end of constructor.i think you doesnt echo anything in controller , so ajax response is null.
whioch framework u use?
I dont think thats going to produce valid JSON. If you want an actual array with numeric keys then use numeric keys in the PHP array:
echo json_encode(array(
1 => 'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
2 => 'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
OR
echo json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
which will output the following js:
[
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
]
In your /controllers/ajax.php file are you running your functions?
class ajax_controller {
function __construct(){
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'Newborn' : return $this->Newborn();
break;
}
}
}
/*
* Process Newborn Gallery request.
*/
public function Newborn(){
header('Content-type: application/json');
return json_encode(array(
'images/gallery/albums/newborn/kylie/thumbnail/kylie-album.jpg',
'images/gallery/albums/newborn/payton/thumbnail/payton-1-thumbnail.png'
));
}
}
$controller = new ajax_controller;
echo $controller->__construct();
you are using part of a url
url: '/controllers/ajax.php',
try using your full url
like
var baseurl = 'http://www.example.com';
url: baseurl+'/controllers/ajax.php',
EDIT
try changing
header('Content-type: application/json')
to
header('Content-type: text/json');
or
header('Content-type: text/plain');
https://stackoverflow.com/questions/267546/correct-http-header-for-json-file
I finally dug around the headers and realized the problem was that .ajax() was sending my PHP script an empty array.
The fix was to get rid of contentType: 'application/json; charset=utf-8, and the header() function in the PHP script.
It now sends and receives data just fine. (goes to read up on configuration docs)
Thanks for all the help - my script would;ve been broken for other reasons w/o many of your answers :)
I got the rest of the library working fully, just trying to generate api keys and its throwing a 403 forbidden when executed via ajax.
({"status":false,"error":"Invalid API Key."})
I traced it to _remap function under REST_Controller.. almost as if im calling the url incorrectly?
workflow: user visits site1.com -> registers for account -> generates api key for their domain -> key recorded in db -> key displayed
The following form would be on site1.com after they register for an account they would click "generate key".
ajax call:
/**
* Generate an API Key for Us to use
*/
$("#submitGetApiKey").click(function(){
$.ajax({
url: "http://dev.site1.com/api/key",
crossDomain: true,
type: "PUT",
dataType: "jsonp",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
for (var i = keys.length - 1; i >= 0; i--) {
console.log(keys[i]);
};
}
});
});
REST-SERVER on GitHub: https://github.com/philsturgeon/codeigniter-restserver
look specifically at key.php under application/controllers/api/key.php
Snippet of the key.php file that should relate to this process:
/**
* Key Create
*
* Insert a key into the database.
*
* #access public
* #return void
*/
public function index_put()
{
// Build a new key
$key = self::_generate_key();
// If no key level provided, give them a rubbish one
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
// Insert the new key
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
{
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
}
else
{
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
}
}
Response/Request Headers
Request URL:http://dev.mapitusa.com/api/key
Request Method:PUT
Status Code:403 Forbidden
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e165df34aa4fda5936e940658030f83d%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328291821%3B%7Dac0f163b112dbd3769e67f4bb7122db2
Host:dev.mapitusa.com
Origin:http://dev.mapitusa.com
Referer:http://dev.mapitusa.com/api_test.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.3 Safari/535.19
Response Headersview source
Cache-Control:max-age=0, public
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:69
Content-Type:application/json
Date:Fri, 03 Feb 2012 18:03:54 GMT
Expires:Fri, 03 Feb 2012 18:03:54 GMT
Keep-Alive:timeout=5, max=98
Server:Apache
Set-Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22f2f466f7b97b89f2a9b557d2d9a0dbcc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328292234%3B%7D6821b96c7e58b55f1767eb265ffdb79e; expires=Fri, 03-Feb-2012 20:03:54 GMT; path=/
Status:403
Vary:Accept-Encoding,User-Agent
X-Powered-By:PHP/5.3.6
X-UA-Compatible:IE=Edge,chrome=1
i ended up finding out the 403 forbidden was because i was not providing an api key to generate keys..
Kind of abiguous as Phil's documentation doesn't state that an existing api key is required before you can generate keys..
I simply created a bogus key in the table in the db and referenced that when calling /key/index?X-API-KEY=boguskey
I have solved the problem of generating the api key.
I'm using Phil Sturgeon's REST API server.
Call the key controller using ajax call as such :
$("#submitGetApiKey").click(function(){
$.ajax({
url: "http://sitename.com/api/key/index?X-API-KEY=your_key_here",
crossDomain: true, /* remove this if using the same domain*/
type: "PUT",
dataType: "jsonp",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
},
success: function(data){
for (var i = keys.length - 1; i >= 0; i--) {
console.log(keys[i]);
};
}
});
});
Inside key controller:
Search for function _generate_key() and check for $this->load->helper('security');. the security helper must be loaded for working of do_hash otherwise you will get 500 internal server error.
public function index_put()
{
// Build a new key
$key = self::_generate_key();
// If no key level provided, give them a rubbish one
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
// Insert the new key
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
{
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
}
else
{
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
}
}
Also, you may call http://sitename.com/api/keyindex?X-API-KEY=your_key_here in your browser's address bar by making a small change in your key controller
you can replace the function name index_put with index_get.
Thanks
If you are calling this from a different domain, you may be running into some XSS issues. You might have to run it from your own server and call the function from it's own domain or possibly use the JSONP capability.
UPDATE: Are you able to see the transaction in Firebug using the NET Tab?
Do you get JSON Back?
Sometimes you have to add callback=? to the url request:
http://dev.site1.com/api/key?callback=?
Update2: Are you able to bring the page up in the browser: (http://dev.mapitusa.com/api/key)
If you get the same error, you should try giving 777 (full read/write) permissions to the site.
This sounds like it might be a browser issue. Maybe an incorrect implementation of PUT in the XMLHttpRequest stack.
I would try converting it quickly to POST just to see if it works. It might be better off leaving it as POST anyway just for compatibility purposes.
I am trying to create a little ajax chat system (just for the heck of it) and I am using prototype.js to handle the ajax part.
One thing I have read in the help is that if you return json data, the callback function will fill that json data in the second parameter.
So in my php file that gets called I have:
header('Content-type: application/json');
if (($response = $acs_ajch_sql->postmsg($acs_ajch_msg,$acs_ajch_username,$acs_ajch_channel,$acs_ajch_ts_client)) === true)
echo json_encode(array('lastid' => $acs_ajch_sql->msgid));
else
echo json_encode(array('error' => $response));
On the ajax request I have:
onSuccess: function (response,json) {
alert(response.responseText);
alert(json);
}
The alert of the response.responseText gives me {"lastid": 8 } but the json gives me null.
Anyone know how I can make this work?
This is the correct syntax for retrieving JSON with Prototype
onSuccess: function(response){
var json = response.responseText.evalJSON();
}
There is a property of Response: Response.responseJSON which is filled with a JSON objects only if the backend returns Content-Type: application/json, i.e. if you do something like this in your backend code:
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($answer));
//this is within a Codeigniter controller
in this case Response.responseJSON != undefined which you can check on the receiving end, in your onSuccess(t) handler:
onSuccess:function(t) {
if (t.responseJSON != undefined)
{
// backend sent some JSON content (maybe with error messages?)
}
else
{
// backend sent some text/html, let's say content for my target DIV
}
}
I am not really answering the question about the second parameter of the handler, but if it does exist, for sure Prototype will only provide it in case of proper content type of the response.
This comes from Prototype official :
Evaluating a JavaScript response
Sometimes the application is designed
to send JavaScript code as a response.
If the content type of the response
matches the MIME type of JavaScript
then this is true and Prototype will
automatically eval() returned code.
You don't need to handle the response
explicitly if you don't need to.
Alternatively, if the response holds a
X-JSON header, its content will be
parsed, saved as an object and sent to
the callbacks as the second argument:
new Ajax.Request('/some_url', {
method:'get', onSuccess:
function(transport, json){
alert(json ? Object.inspect(json) : "no JSON object");
}
});
Use this functionality when you want to fetch non-trivial
data with Ajax but want to avoid the
overhead of parsing XML responses.
JSON is much faster (and lighter) than
XML.
You could also just skip the framework. Here's a cross-browser compatible way to do ajax, used in a comments widget:
//fetches comments from the server
CommentWidget.prototype.getComments = function() {
var commentURL = this.getCommentsURL + this.obj.type + '/' + this.obj.id;
this.asyncRequest('GET', commentURL, null);
}
//initiates an XHR request
CommentWidget.prototype.asyncRequest = function(method, uri, form) {
var o = createXhrObject()
if(!o) { return null; }
o.open(method, uri, true);
o.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
var self = this;
o.onreadystatechange = function () {self.callback(o)};
if (form) {
o.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
o.send(makePostData(form));
} else {
o.send('');
}
}
//after a comment is posted, this rewrites the comments on the page
CommentWidget.prototype.callback = function(o) {
if (o.readyState != 4) { return }
//turns the JSON string into a JavaScript object.
var response_obj = eval('(' + o.responseText + ')');
this.comments = response_obj.comments;
this.refresh()
}
I open-sourced this code here http://www.trailbehind.com/comment_widget