$.ajax fire never being read as done?

Jaxel

Well-known member
I'm trying to do some simple AJAX, that records some data... this is my javascript:
Code:
$.ajax({
    url: '*****/session',
    data: { url: encodeURIComponent(window.location.href) },
    dataType: 'jsonp'
}).done(function(data)
{
    Twitch.login({
        scope: ['user_read', 'user_follows_edit', 'user_subscriptions'],
        redirect_uri: '*****/authorize',
    });
});
Then this is my PHP code behind; which is working fine:
Code:
public function actionSession()
{
    $ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));
    $url = urldecode($this->_input->filterSingle('url', XenForo_Input::STRING));
   
    XenForo_Application::get('db')->query('
        INSERT INTO EWRcanal_session (ip, url) VALUES (?, ?)
        ON DUPLICATE KEY UPDATE url = VALUES(url)
    ', array($ip, $url));
   
    $this->_routeMatch->setResponseType('json');
    return $this->responseView('EWRcanal_ViewPublic_Session', '');
}

The issue is coming from the javascript. It executes the ajax fine, and the data gets inserted into my database... however, the code inside of the ".done()" never gets fired. For some reason, the ajax is never returning a successful response. Anyone got any ideas?
 
If you're using Chrome, open your javascript console, click the network tab, and then trigger your ajax call. You'll be able to see the request information in that window (the path, the method [get|post], the status [200|403|404|500 etc]) clicking the request you want will show the response information along with headers. I'm guessing that you're erroring at some point.

You can do this with Firefox as well, I'm just not sure where you find that tab.

[Edit] After looking at your php, I'm thinking it's most likely the view that's erroring. You have to make a view with renderJson() function for json requests or the view will say it can't be delivered in that method. This would explain why your db insert is loading but the javascript doesn't 'finish'.
 
My view is as follows... chrome inspector is reporting no errors...
Code:
<?php

class EWRcanal_ViewPublic_Session extends XenForo_ViewPublic_Base
{
    public function renderJson()
    {
        $output = $this->_renderer->getDefaultOutputArray(get_class($this), $this->_params, $this->_templateName);
        return '('.XenForo_ViewRenderer_Json::jsonEncodeForOutput($output).')';
    }
}
 
I'm not 100% but I think adding ()s around your json makes it an invalid json string which may be why jquery won't execute the .done() function.
 
Well then add

Code:
.fail(function(XHR, status, error){
console.log(error);})
after the .done() part and see if it's in fact failing or if something else is happening.
 
Ah... thanks... figured it out from that. I had to include the callback function name in front of the (). It can't run anonymously apparently.
 
Top Bottom