Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:16:37+00:00 2026-06-03T23:16:37+00:00

NOTE : I’ve pasted more code than just the ajax calls, on the off

  • 0

NOTE: I’ve pasted more code than just the ajax calls, on the off chance that code is (part of) what’s causing the problem. I don’t think it is, however, so you’re probably better off focussing on the ajax and jAjax functions a bit further down.
Also note that, since there’s a comment (with upvote) on this question saying my code is hard to decipher, I’d happily clarify what needs clarifying if that could prove to be the key in finding the problem.
Thanks.


Here’s the thing. I’m trying to ditch jQuery, since the only thing I use is the $.ajax() method, and including an entire lib like jQuery for just 1 feature is IMO crazy. I don’t even need the full functionality of the $.ajax method anyway, hence I wrote my own ajax function.

The problem is: it’s not working, and I can’t seem to figure out why. I’m trying to send objects to the server (specifically: ajaxAction in the controller – using Zend FW). Below is the javascript code, and a summary of what the firebug console tells me.

if (!String.prototype.trim)
{
    String.prototype.trim = function()
    {
        "use strict";
        return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    };
}

function getUrl(action,controller)
{
    var base,uri;
    base = window.location.href.replace('http://'+window.location.host,'');
    if (base.length > 1)
    {
        base = base.substring(1,base.length).split('/');
        controller = controller || base[0];
        base[0] = controller || base[0];
        base[1] = action || base[1];
        return '/'+base.join('/');
    }
    controller = controller || 'index';
    action = action || 'ajax';
    return base+controller+'/'+action;
}

function formalizeObject(obj,recursion)
{
    recursion = recursion || false;
    if (typeof obj !== 'object')
    {
        throw new Error('no object provided');
    }
    var ret = '';
    for (var i in obj)
    {
        if (!obj.hasOwnProperty(i) || typeof obj[i] === 'function')
        {
            continue;
        }
        if (recursion)
        {
            ret +='['+i+']';
        }
        else
        {
            ret += (ret.length > 0 ? '&' : '') + i.toString(); 
        }
        if (typeof obj[i] === 'object')
        {
            ret += formalizeObject(obj[i],true);
            continue;
        }
        ret += '='+obj[i].toString();
    }
    if (recursion)
    {
        return ret;
    }
    return encodeURI(ret);
}

function success()
{
    if (this.readyState===4 && this.status===200)
    {
        console.log(this.responseText);
    }
}

function ajax(str,url,method,json)
{
    var ret;
    json = json || false;
    str = str || {};
    method = method || 'POST';
    url = url || getUrl();
    str = 
    str = (typeof str === 'object' ? str : {data:str});
    try
    {
        ret = new XMLHttpRequest();
    }
    catch (error)
    {
        try
        {
            ret= new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(error)
        {
            try
            {
                ret= new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(error)
            {
                throw new Error('no Ajax support?');
            }
        }
    }
    if (typeof ret !== 'object')
    {
        throw new Error('No Ajax, FFS');
    }
    ret.open(method, url, true);
    ret.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    ret.setRequestHeader('Content-type', (json ? 'application/json' : 'application/x-www-form-urlencode'));
    ret.onreadystatechange = success;
    ret.send((json ? JSON.stringify(str) : formalizeObject(str)));
    return true;
}

function jAjax(str,url)
{
    $.ajax(
    {
        url : url,
        data: str,
        type: 'POST',
        success: function(res)
        {
            console.log(res);
        }
    });
}

Four ways in which I’ve tried to make the Ajax request:

jAjax({data:{foo:'bar'}},getUrl());//1
jAjax({data:{foo:'bar'}},getUrl(),true);//2
ajax({data:{foo:'bar'}},getUrl());//3
ajax({data:{foo:'bar'}},getUrl(),true);//4
  1. jAjax({data:{foo:'bar'}},getUrl());: This works just fine:

    []{“ajax”:true,”controller”:”index”,”action”:”ajax”,”module”:”default”,”identity”:{},”data”:{“foo”:”Bar”}}
    Parameters: data[foo] ‘bar’ And Source: data%5Bfoo%5D=Bar (from POST tab in FB console)
    Header: application/x-www-form-urlencoded; charset=UTF-8
    All of this was sent to the following url: http://www.foo.bar/index/ajax?data%5Bfoo%5D=bar

  2. This doesn’t work, however:

    []{“ajax”:true,”controller”:”index”,”action”:”ajax”,”module”:”default”,”identity”:{}} is the response
    POST tab in FB: JSON data: {foo:’Bar’} source: {“data”:{“Foo”:”Bar”}} (but same url is case 1)
    Header: json; charset=UTF-8

  3. This is the big one: the full request url is identical to url from case 1, as are the headers BUT when I look at the POST tab in the FB console (inspect the request) This is the only difference I can find:

    case 1: Parameters: data[foo] ‘bar’ Source: data%5Bfoo%5D=Bar
    In this case, I can’t see the Parameters section, only: Source: data%5Bfoo%5D=Bar

  4. Identical to case2, except for the url, which I think I forgot to pass through encodeURI. This case is less important for now. I think/hope I’ll get this working the moment I figure out what’s wrong with case 3.

In all 4 cases, the request is sent, and received. The controller action is as follows:

public function ajaxAction()
{
    $this->_helper->layout->disableLayout();
    $this->getHelper('viewRenderer')->setNoRender();
    $this->_helper->getHelper('AjaxContext')->addActionContext( 'ajax' , 'json' )
                                            ->initContext('json');
    if($this->getRequest()->isPost() && $this->getRequest()->isXmlHttpRequest())
    {
        echo json_encode(array_merge(array('ajax'=>true),$this->_getAllParams()));
    }
    else
    {
        throw new Exception('no ajax call made??');
    }
}

Since I’m receiving a JSON string, I’m sure the request is posted, and has the correct XMLHttpRequest header. Why then, can’t I post JSON objects? Even more to the point: why is case 3 not working? What is jQuery doing that I’m not aware of? What is it, that makes case 1 to work, but not case 3?

PS: It might be irrelevant, but in a moment of madness I tried adding this: ret.setRequestHeader('Connection','close'); to the ajax function, but I noticed that, in the header that got sent out, Connection was set to keep-alive all the same. Perhaps this gives someone a clue as to what went wrong?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:16:39+00:00Added an answer on June 3, 2026 at 11:16 pm

    In case anybody wonders what was wrong:

    ret.setRequestHeader('Content-type', 'application/x-www-form-urlencode');
    

    Should have been “x-www-form-urlencoded”, with a “d” in the end:

    ret.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    

    sending a formalized object is now working, and I can get rid of jQuery 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note that the following code is in a class a single class private string
Note : The code in this question is part of deSleeper if you want
Note: the below code is just for demonstration and I am using jQuery and
Note: I'm more interested in understanding general Go concepts/patterns, rather than solving this contrived
Note: Already asked in this post (asked on StackOverflow) . However, the problem is
Note: Before you spend your time reading on, please know that C2DM is itself
Note: PopupMenu is available with API level 11 and higher. http://developer.android.com/guide/topics/ui/menus.html#PopupMenu With that in
NOTE: Please, don't code it for me. I already have a home-rolled function to
Note that singleton used in slightly uncommon sense - object visible as single instance
NOTE : My specific problem is solved (explained at the bottom), but I'll leave

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.