Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    44
    Thanks
    21
    Thanked 1 Time in 1 Post

    Cannot get simple jQuery AJAX to work

    I'm getting very frustrated with jQuery and AJAX. I have looked at many examples and have tried most of them, but I cannot get them to work. Here is a very simple AJAX I've tried:

    SERVER CODE for returning variables:
    PHP Code:
    foreach($_REQUEST as $reqkey => $reqval)
      {
      echo 
    "<br />REQUEST item $reqkey has a value of $reqval<br />";
      }
    echo 
    "END RETURN STRING"
    Sample jQuery code:
    Code:
    $(document).ready(function(){
      $('#doAjax').click(function(){
        alert('Button was clicked');
        $.ajax({
          type: "POST",
          url: "http://localhost/test5/process.php",
          data: {test: '1'},
          contentType: "text",
          dataType: "text",
          success: function(data){
            alert("Successful POST.  Data returned is " + data);
          } // end success: function(data){
        });// end $.ajax({
      }); // end $('#doAjax').click(funtion(){
    }); // end $(document).ready(function(){
    When I click the button, I get the alert that the button is pressed, but I get no alert that the data has posted, nor any return data. Can someone please tell me what I'm doing wrong?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,180
    Thanks
    23
    Thanked 602 Times in 601 Posts
    This line
    Code:
    contentType: "text",
    is wrong and should be removed. Then it will work.
    BUT. I think the author wanted the alert to have those line breaks so I would substitute
    Code:
    \n
    for the <br />
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •