1

When attempting to use the JQuery Ajax post function, I have exhausted all the options I could think of for it to work. I just need to post the data to a PHP handler and pass 2 variables. I confirmed the function is being called fine when I comment out the post code (alert is called), but as soon as I uncomment the line, nothing happens. I am using Firefox but have also tried it with Chrome as well.

<script type="text/javarscript">
    function removeDatacenter( datacenter_id ) {    
        alert( datacenter_id );
        $.ajax({ 
           type: "POST", 
           url: "handler.php", 
           data: { action-type: 'remove_datacenter', id: '2' }
        });
    };
</script>
6
  • specify the FULL path in the "url" parameter Commented Aug 1, 2013 at 8:35
  • 2
    @Th0rndike Where did you get the idea it only accepts absolute paths? Commented Aug 1, 2013 at 8:35
  • I've had trouble when specifying relative paths. What does your handler look like, anyway? Commented Aug 1, 2013 at 8:37
  • Well, it does accept relative paths. Perhaps you are experiencing some bugs, but it does. Always read the documentation to be sure. Commented Aug 1, 2013 at 8:48
  • sorry edited your post by mistake Commented Aug 1, 2013 at 8:52

2 Answers 2

3

You have to wrap action-type key object inside quotes because of '-' character:

You could rename it to ,e.g, actionType

$.ajax({ 
 type: "POST", 
 url: "handler.php", 
 data: { 'action-type': 'remove_datacenter', id: datacenter_id } 
})
Sign up to request clarification or add additional context in comments.

5 Comments

Why only action-type and not "id" as well? Just out of curiosity of course, trying it now.
@BrettPowell no need to wrap id. You have to wrap key name when using special character as '-'
action-type (without quotes) will try to evaluate the expression action minus type. But this will fail. There should be an exception in your Javascript console, though.
$.ajax().done(function(data){/*use data here*/}); You have to return something from your php script of course and then you can use 'data'. Here your ajax method will wait for data type text/html (default), if you want to get data in other format, you can specify it using ajax option dataType or use more specific ajax method as $.getJSON() e.g
Thanks, was confused a bit. I have the .done(function(result)) and the alert box shows but all paths return a value in the handler (just dummy stuff like "YES", "NO") but the alert box shows an empty value when printing result.
1
$.ajax({ 
    type: "POST", 
    url: "handler.php", 
    data: { 
        'action-type': 'remove_datacenter', 
        id: 2
    } 
});

1 Comment

What is the difference between your and roasted's answer?

Your Answer

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