0
function updateRecord(actdate, plandate, bugnumber) {
    var adate = document.getElementById(actdate).value.split("-");
    var pdate = document.getElementById(plandate).value.split("-");
            jQuery.ajax({
                url: '?=bug_parser/updaterecord/'+adate[0]+'/'+adate[1]+'/'+adate[2]+'/'+pdate[0]+'/'+pdate[1]+'/'+pdate[2]+'/'+bugnumber,
                dataType: 'html',
                async:true,
                crossDomain:true,
                success: function(response, textStatus, jqXHR) {
                    if(response == '1') {
                        alert("success");
                    } else {
                        alert("Failed");
                    }       
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('Error updating the record');
                },
            });
    return false;
} 

I am trying to update a record through ajax call on button click event. Have proper menu call backs in my drupal module. Interesting thing is that, if I copy paste the url in browser navigation bar, it works fine as expected. But if I make ajax call through javascript it doesnot work

grssnbchr
  • 2,877
  • 7
  • 37
  • 71
haranadh
  • 59
  • 1
  • 3
  • Some questions first: Why do you use crossDomain: true? Is this necessary? Is the onError function executed, or what happens exactly? Please consider using a Debugger, such as the Developer Tools in Chrome, to actually see WHAT is getting sent to the server and WHAT is coming back. I suppose, since you say pasting the url in the browser works, nothing is actually sent because you have problems with your JS. – grssnbchr Nov 30 '12 at 07:19

1 Answers1

0

The reason your ajax is not working is because you need to put it inside a drupal behavior.

Lookup drupal js behaviors. It's basically creating a wrapper for your js so it works well with the drupal javascript files and includes.

You can also use this topic as a reference for behaviors: Drupal behaviors

Normally all your javascript has to go inside a behavior. It's best practice and saves you a lot of headaches

Community
  • 1
  • 1