1

I'm building an AJAX form and I'm trying to send 3 fields by JSON.

Client-side, the form is serialised and entered into JSON format:

$('#form-signin').live('submit', function(event) {
    var target = $('#ajax');
    var url = '/ajax/user/authenticateLevel2';

    $.ajax({

        type: "POST",
        url: url,
        data: $.base64.encode($('#form-signin').serialize()),
        dataType: 'json',

        success: function(data, status) {
            $.getJSON(url, function(data) {
                $('#ajax').html($.base64.decode(data.html));
                $('#ajax').modal();
            });
        }
    });
    event.preventDefault();
});

Server side, my router splits the URL request up, sees that the first part contains 'ajax' then proceeds to specially pass the routing request to an AJAX handler.

my problem is that even inside the router, checking $_REQUEST, which is what is used to get the information about the post, the post data is not there. The same goes with $_POST.

Even the first page where the request hits (index.php), $_REQUEST does not have the data.

What am I doing wrong?

Server Side,

The request is sent to an index.php which includes the Autoloader and init script.

The init script initialises the database connection, sets the error, exception and session handling, then passes the request onto the router.

The router, in its construction method: sets the URL as an array (exploded $_SERVER['REQUEST_URI']), and then sets the relevant controller, method and additional parameters.

In this case, as we are doing an ajax request, special processing happens before we dispatch the request.

The method parameters are set to:

    $requestParams = $_REQUEST;
    unset($requestParams['url']);

This request parameter(s) along with additional information (url, controller, method and database object) are passed for dispatch.

In all cases, we are primarily dispatching using this method:

            $dispatchedController = new $this->controller($this->database);
            $method = $this->method;

            return $dispatchedController->$method($this->params);

3 Answers 3

1

If I remember right from using a plugin a long time ago, the method $.base64.encode() returns a single string so what you are probably sending to the server is something like a single parameter with no value.

I believe you should be doing something like

data: "foo=" + $.base64.encode($('#form-signin').serialize()),
Sign up to request clarification or add additional context in comments.

Comments

0

You are not sending json to the server just a base64 encoded string. Also you are expecting key/pair values. To send key/pair values just pass the serialized form data to the $.ajax function.

$('#form-signin').live('submit', function(event) {
    var target = $('#ajax');
    var url = '/ajax/user/authenticateLevel2';
    $.ajax({
        type: "POST",
        url: url,
        data: $('#form-signin').serialize(),
        dataType: 'json',
        success: function(data, status) {
            $.getJSON(url, function(data) {
                $('#ajax').html($.base64.decode(data.html));
                $('#ajax').modal();
            });
        }
    });
    event.preventDefault();
});

Comments

0

The code should work (assuming your HTML is not the problem here, e.g., '#form-signin' is the right selector for the right form).

You mentioned you are not able to get the data on the server side. However, are you absolutely sure you are even sending the data you need from the client? For example, have you analyzed the request using a tool such as Firebug?

3 Comments

Yep, firebug sees the json in the headers, so it's definitely being send client-side. Even decoding the JSON string, I get the form input I put in.
@Shamil It sounds like you should be posting your server-side code then since the client is doing what is supposed to be doing.
I have added a basic description of the process in the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.