1

Having trouble setting up a website for my wedding. Hope someone can find out what the problem is before the big day :)

The following code is working on my dev machine but once I publish it to my server, it does the postback and then imediatly after performs a GET request. The GET request should not happen.

I've got some basic set up code

$.ajaxSetup({
        beforeSend: function (xmlHttpRequest) {

//Used by the server to determine an async request
            xmlHttpRequest.setRequestHeader('X-Requested-With', 'jQuery');

            $.blockUI({ message: '<h3>Please wait...</h3>' });
        },
        complete: function (XMLHttpRequest, textStatus) {
            $.unblockUI();
        }
    });

And here is the code that binds to the anchor tag. Note that I've also tried this on button and input elements with no luck.

$('.basket-btn').click(function (src) {

        src.preventDefault();

        var form = $(src.target).closest('form');

        /* If the form contains validatable elements, then set this to false so the validation function is triggered. */
        var doWeValidate = !form.find('span[id$=_validationMessage]').length > 0;

        if (doWeValidate || !Sys.Mvc.FormContext.getValidationForForm(form[0]).validate('submit').length) {

            var destination = $(src.target).attr('data-destination');

            $.post(form.attr('action'), form.serialize(), function (data) {

                $("#" + destination).html(data);
            });
        }
    });

Here is my site if you want to see the problem I'm talking about.

http://www.vinceandjeanswedding.com/GiftRegistry/

If you enter an amount and click contribute, it will perform a postback and then (according to firebug) a GET request will follow straight after.

Please help me.

3 Answers3

1

I worked this out.

I noticed that in firebug, the first requrest always returned a 302 Object Moved error.

Simply placing a forward slash on all my requests fixed the problem.

So instead of www.site.com/Home it is now www.site.com/Home/

  • I have also seen cases where the IIS server would trigger a 302 Object Moved if the casing of the request did not match. To avoid this always use lowercase everywhere, when setting up the service and when making the requests. – AnthonyVO Feb 27 '13 at 15:47
0

I see that the first time it does work fine (in Chrome). When the data comes back , you refresh all the stuff in the form. Now what happens is that the (new) contribute buttons are not bound to any event.

You want to change the following

$('.basket-btn').click(function (src) {

into

$('.basket-btn').live('click', function (src) {

So jQuery will catch all .basket-btn events, even if they are added after the first binding.

UPDATE

In Firefox the bug does occur straight away.

You have some weird things going on. Your ajax does seem to work. But it loads the whole page again in ajax, (including all the script references), causing after a while a huge loop to occur (it increasingly loads (#contributions * #contributions times) the scripts) .

Gideon
  • 18,251
  • 5
  • 45
  • 64
  • Thanks for your time. I only want the inner html of the div #shopping-basket-info to be replaced. The '.basket-btn' are not meant to be replaced. –  Jan 31 '11 at 11:24
  • I don't want to get into details, just a small note, maybe it helps: FireFox does not prevent custom headers after redirect. So after redirect the request will not look like it's AJAX, X-Requested-With stripped off. http://stackoverflow.com/questions/1588589/firefox-does-not-preserve-custom-headers-during-ajax-request-redirect-an-asp-net – queen3 Jan 31 '11 at 19:51
0

Just remove

cookieless="AutoDetect"

in your web.config file.

Khurram Shaikh
  • 300
  • 2
  • 14