1

I just want to send address information to drupal for check content existence through the AJAX.

Request is sending successfully and callback function also executing. but I am not getting any data in $_POST array.

here is my AJAX code.

    var address = [{'add1':'address line1','add2':'address line2','add3':'address line3'}]
    $.ajax({
            url: Drupal.settings.property_check.base_url + '/search/property-by-address',
            type: 'POST',
            data: JSON.stringify(address),
            headers:{
              'Content-Type': undefined,
            }
        })
        .done(function(response) {
            console.log(response);
        })

This is callback routing & function

  $items['search/property-by-address'] = array(
     'title' => 'property by address',
     'page callback' => 'search_property_by_address',
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );

  return $items;

function search_property_by_address(){
  drupal_json_output($_POST);

}

At where I am making mistake? is there any other way? Or is there any problem with headers?

1
  • 3
    Why are you converting the address object to a string before sending the post? Also it should be an object rather than an array. That's probably the issue
    – Clive
    Commented Sep 21, 2016 at 8:17

1 Answer 1

4

Try something like this:

$.ajax({
   url: Drupal.settings.property_check.base_url + '/search/property-by-address',
   type: 'POST',
   data: {
       'add1':'address line1',
       'add2':'address line2',
       'add3':'address line3'
   },
   headers:{
       'Content-Type': undefined,
   }
}).done(function(response) {
        console.log(response);
})

or

 var address = {'add1':'address line1','add2':'address line2','add3':'address line3'};

 $.ajax({
     url: Drupal.settings.property_check.base_url + '/search/property-by-address',
     type: 'POST',
     data: address,
     headers:{
         'Content-Type': undefined,
     }
 }).done(function(response) {
     console.log(response);
 })

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.