views:

30

answers:

1

I have written some code using jQuery to use Ajax to get data from another WebForm, and it works fine. I'm copying the code to another project, but it won't work properly. When a class member is clicked, it will give me the ProductID that I have concatenated onto the input ID, but it never alerts the data from the $.get. The test page (/Products/Ajax/Default.aspx) that I have set up simply returns the text "TESTING...". I installed Web Development Helper in IE, and it shows that the request is getting to the test page and that the status is 200 with my correct return text. However, jQuery refreshes my calling page before it will ever show me the data that I'm asking for. Below are the code snippets from my page. Please let me know if there are other code blocks that you need to see. Thank you!

 <script type="text/javascript">
        $(document).ready(function() {
            $(".addtocart_a").click(function() {

                var sProdIDFileID = $(this).attr("id");
                var aProdIDFileID = sProdIDFileID.split("_");
                var sProdID = aProdIDFileID[5];
                // *** This alert shows fine -- ProdID: 7
                alert("ProdID: " + sProdID);

                $.get("/Products/Ajax/Default.aspx", { test: "yes" }, function(data) {
                    // *** This alert never gets displayed
                    alert("Data Loaded: " + data);
                }, "text");
            });
        });    
    </script>

<input src="/images/add_to_cart.png" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$aAddToCart_7" type="image" id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_aAddToCart_7" class="addtocart_a" />
+3  A: 

The easiest way is to tell jQuery not to return anything.

$(".addtocart_a").click(function(e){

  // REST OF FUNCTION

  return false;
});

Good luck! If you need anything else let me know.

ewakened
This did work! This also worked: e.preventDefault(); Why is it that I didn't have to do that on my last project?
Jeremy Sullivan
Excellent! Glad to hear it and good to know :)
ewakened