Results 1 to 6 of 6
  1. #1

    Default Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    Hello,

    I'm developing an encapsulated plugin for Zen Cart 2.1 and have run into a persistent AJAX routing issue.

    My plugin successfully injects JavaScript onto the page, and that script makes a standard POST request to ajax.php with an action and method (e.g., ajax.php?act=MyAction&method=myMethod). The POST data correctly includes the securityToken.

    When the call is made, the server responds with a 400 Bad Request. This indicates the ajax.php router is receiving the request but rejects the action as invalid.

    My AJAX handler class (e.g., class zcAjaxMyAction) and its corresponding file (zcAjaxMyAction.php) are located in my plugin's .../catalog/includes/classes/ajax/ directory. The class and file names appear to follow the standard zcAjaxPascalCase convention.

    What is the definitive method for an encapsulated plugin to register its AJAX handler classes so they are discoverable by the ajax.php router? The automatic discovery does not seem to be working, and my attempts to manually register the path using an auto_loaders config file have failed.

    Any insight into the correct process for ZC 2.1 would be greatly appreciated. Thank you!
    Case Badges by Techiant

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,436
    Plugin Contributions
    94

    Default Re: Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    It'll help if you could post the contents of your javascript/jquery and ajax class for review. If you're hesitant to post directly, send them to me via PM.

  3. #3

    Default Re: Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    Quote Originally Posted by lat9 View Post
    It'll help if you could post the contents of your javascript/jquery and ajax class for review. If you're hesitant to post directly, send them to me via PM.
    Oh gosh! After smashing my head against this for several hours, I decided to just extensively test ajax.php and log every step. It turns out the issue was that I was setting my method as my_method which in ajax.php was failing at line 51:
    PHP Code:
    if (!isset($_GET['act'], $_GET['method']) || !preg_match('/^[a-zA-Z0-9]+$/'$_GET['act']) || !preg_match('/^[a-zA-Z0-9]+$/'$_GET['method'])) 
    I feel super dumb for not paying attention to that regex for $_GET['method'] but relieved that finally made the connection :)
    Case Badges by Techiant

  4. #4
    Join Date
    Jul 2012
    Posts
    16,806
    Plugin Contributions
    17

    Default Re: Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    Quote Originally Posted by Techiant View Post
    Oh gosh! After smashing my head against this for several hours, I decided to just extensively test ajax.php and log every step. It turns out the issue was that I was setting my method as my_method which in ajax.php was failing at line 51:
    PHP Code:
    if (!isset($_GET['act'], $_GET['method']) || !preg_match('/^[a-zA-Z0-9]+$/'$_GET['act']) || !preg_match('/^[a-zA-Z0-9]+$/'$_GET['method'])) 
    I feel super dumb for not paying attention to that regex for $_GET['method'] but relieved that finally made the connection :)
    Didn't spend quite the same amount of time; however, have found this narrowly applied section of code causes unnecessary issues with other ajax plugins/code. Haven't a clue why an underscore wasn't factored into an allowed character affecting the filename, the class name, and any associated class method. Sure, PSR-1 might be a reason to address/remove it for the class name, but for existing code that wasn't subject to the unrelated issues of arrays and such, it unnecessarily breaks things.

    I'm working on reporting the issue in github since it was already somewhat identified as an issue against the original PR but no closure/further reference provided. I'm also trying to find a post asking about use of an associated plugin so I could report to them that no, as written the plugin code has to be modified because of the oversanitization applied in ZC 2.1.0.

    Fun part moving forward will be how to support updates to the plugin because now some files have to be modified, renamed *and* "deleted" while some only need modification just to handle this new unnecessary requirement.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  5. #5

    Default Re: Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    Agreed. I honestly thought there was something wrong with my query, sessions, etc. I mean I have no problem with following new standards, just wish the ajax.php was more verbose about it than just die with 400 status. At least I know where to look next time.
    Case Badges by Techiant

  6. #6
    Join Date
    Jul 2012
    Posts
    16,806
    Plugin Contributions
    17

    Default Re: Encapsulated Plugin AJAX Call Fails with 400 Bad Request in ZC 2.1

    Quote Originally Posted by Techiant View Post
    Agreed. I honestly thought there was something wrong with my query, sessions, etc. I mean I have no problem with following new standards, just wish the ajax.php was more verbose about it than just die with 400 status. At least I know where to look next time.
    There's a number of places where additional verbosity would have been appreciated, more so in github than specifically in the file (though at least some level of "notification" to user is nice).

    One needs to also think about what information is given to the malicious user testing the site... Ohh, a bad name? Fine, I'll just change to this... Oh, good name, but.... I mean yeah there's the open-source aspect, just saying failure is failure.

    Why having an underscore is a failure? Not at all described in the solution and not part of what was targeted to "fix". As such, problem was a "feature", not intentional.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

 

 

Similar Threads

  1. 400 Bad Request
    By Coffinwear in forum PayPal Express Checkout support
    Replies: 3
    Last Post: 29 Nov 2010, 06:42 AM
  2. 400 Bad Request when downloading
    By groovyclipart in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 29 Oct 2010, 03:43 PM
  3. Bad Request (400) on Add to Cart in Safari/Chrome
    By cave.dox in forum General Questions
    Replies: 2
    Last Post: 9 Feb 2010, 03:22 PM
  4. Checkout Issue - HTTP 400 Bad Request
    By DPM in forum Templates, Stylesheets, Page Layout
    Replies: 5
    Last Post: 10 Feb 2009, 03:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR