skip to Main Content

I am trying to pass value through an ajax json array but value of catergory variable is not getting in controller action

var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
  type: "POST",
  data: {
    'category': category
  },
  dataType: "json",
  cache: false,
  contentType: false,
  processData: false,
  success: function(response) {}
});

4

Answers


  1. Just make the following changes :

    var category = $('#category').val();
    var url = $('#ajax_action_search').val();
    
    $.ajax({
      type: "POST",
      url:url,
      data: {'category': category},
      dataType: "json",
      success: function(response) {}
    });
    
    Login or Signup to reply.
  2. Please check the Controller action if category (parameter name) passed from the ajax is exactly same in the controller action too

    var category = $('#category').val();
    var url = $('#ajax_action_search').val();
    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        cache: false,
        async: false,
        url: url,
        data: JSON.stringify {
            category: category
        },
        dataType: 'json',
        success: function(response) {}
    });
    
    Login or Signup to reply.
  3. You need to use the parameter namespace matching your extension/plugin:

    $.ajax({
      // ...
      data: {
        'tx_myext_foo[category]': category,
      },
      // ...
    });
    

    But you’ll also need to configure the cHash evaluation since this will lead to a HTTP request like /?tx_myext_foo[category]=X which will fail without a matching cHash.

    This can be done with the excludedParameters configuration option.

    Login or Signup to reply.
  4. You need to make ajaxurl with action and controller. and then pass the data in full format.

    var ajaxUrl = '<f:uri.action action="youraction" controller="Yourcontroller" />';
    var category = $('#category').val();
    
    $.ajax({
        type: 'POST',
        url: ajaxUrl,
        data: '&tx_yourext_yourplugin[category]='+category,
        success: function(response) {
        },
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search