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 8142747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:54:29+00:00 2026-06-06T12:54:29+00:00

I am tring to use an ajax post to an action. GET requests work

  • 0

I am tring to use an ajax post to an action. GET requests work fine but when I try to POST I see a ‘400 Bad Request’ in firebug and the view return a ‘Black hole’ response.

Here is the Jquery request:

            $.ajax({
            url:"/usermgmt/users/editUser",
            type:"POST",
            success:function(data) {
                alert('Wow this actually worked');
                //ko.applyBindings(self);

            },
            error:function() {
                alert('This will never work');
            }
        });

Is this due to the security settings of Cake or what am I missing here?

  • 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-06T12:54:31+00:00Added an answer on June 6, 2026 at 12:54 pm

    Protection against form tampering is one of the basic features provided by the Security Component. As long as it is enabled, it is going to treat all POSTs as form submissions.

    A regular hand-coded HTML form won’t work with the Security Component enabled, so neither will a JQuery-generated POST. You can, of course, use $this->Security->validatePost = false; or $this->Security->csrfCheck = false; but then you loose the protection that the Security Component provides.

    To keep the Security Component on and working as normal, you need to use the CakePHP Form Helper to create the form you’re going to post via ajax. This way the data[_Token][fields] and data[_Token][unlocked] hidden fields get generated with their keys:

    <?php 
        echo $this->Form->create('Test',array('id'=>'testform'));
        echo $this->Form->input('Something');
        echo $this->Form->submit();
        echo $this->Form->end();
    ?> 
    

    This will generate something like this:

    <form action="/your/url" id="testform" method="post" accept-charset="utf-8">
        <div style="display:none;">
            <input type="hidden" name="_method" value="POST"/>
            <input type="hidden" name="data[_Token][key]" value="9704aa0281d8b5a2fcf628e9fe6f6c8410d8f07a" id="Token937294161"/>
        </div>
        <div class="input text">
            <input name="data[Test][Something]" class="required" type="text" id="TestSomething"/>
        </div>
        <div class="submit">
            <input  type="submit" />
        </div>
        <div style="display:none;">
            <input type="hidden" name="data[_Token][fields]" value="0c81fda1883cf8f8b8ab39eb15d355eabcfee7a9%3A" id="TokenFields817327064"/>
            <input type="hidden" name="data[_Token][unlocked]" value="" id="TokenUnlocked281911782"/>
        </div>
    </form>   
    

    Now it’s just a matter of serializing this form in JQuery so that it can be sent with the ajax POST:

        $('#testform').submit(function(event) {
            $.ajax({
                type: 'POST',
                url: "/your/url",
                data: $('#testform').serialize(),
                success: function(data){ 
                    alert('Wow this actually worked');
                },
                error:function() {
                    alert('This will never work');
                }
            });
            event.preventDefault(); // Stops form being submitted in traditional way
        });
    

    Now if you press the submit button, the POST will succeed.

    IMPORTANT: Due to the fact that the Form Helper’s Tokens can only be used with the Security Component once, this solution only works if you only intend to POST once per page generation. If you need to be able to post the same form several times between page reloads then you’ll need to do the following when you add the Security Component at the beginning of your Controller:

    public $components = array(
        'Security' => array(
            'csrfUseOnce' => false
        )
    );
    

    …this will allow the tokens to be used for more than one request. It’s not as secure but you can combine it with csrfExpires so that the tokens will expire eventually. This is all documented in the CSRF configuration section of the Cake book.

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

Sidebar

Related Questions

I'm trying to use jquery.Ajax to post data to an ASP.NET MVC2 action method
I am trying to use .ajax() to post a People object to a MVC2
I'm trying to get some json from an action but I have a problem.
I'm trying to use an ajax contact form to send me emails but i'd
I have a simple div that i use to trigger an ajax post. I
I am trying to use jquery ajax to get data from a php file.
I am currently trying to use AJAX in my application via jRails. I am
I am trying to use periodic refresh(ajax)/polling on my site by XMLHttp(XHR) to check
I am trying to use the Bing Maps Ajax Control to plot pins of
I am trying to make use of jQueryUI AJAX tabs in my ASP.Net Webforms

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.