0

So, I have an ajax post that calls a service and returns an object which I want to parse on success.

I am running this in vs2010, it only works when I step through the ajax post code and it only works part of the time.

This call is basically identical to other pages that I have done so I am really troubled as to why this is not working.

$.ajax({
                type: "POST",
                url: webMethod,
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (obj) {
                    alert("Success");
                    parseStart(obj.d, id, idnum, parseType);
                },
                error: function (e) {
                    alert("Failure. :" + e);
                }
            });

It is really standard, I am not sure why it is doing this. I am just hoping someone has experienced something like this before. Like I said it only goes through if I step through the ajax code and then it gets to the success. If I do not step through it is as if it just never hits success or failure..... It is really strange. (so I do not see my alerts etc if I do not step through it) I had a suggestion that it was possibly the size of my object that I was returning from my service so I trimmed that down to close to nothing and that did not help either.

Any thoughts directions on this are greatly appreciated. I am currently stumped.

Bill Blankenship
  • 3,316
  • 6
  • 43
  • 73
  • I believe that I have found that this is due to post that is happening in an internal method being called within the webmethod a post within a post I am posting another question regarding this as I am unable to delete this question. – Bill Blankenship May 21 '12 at 20:19
  • And didn't want to create a rambling extremely off topic question. – Bill Blankenship May 21 '12 at 20:28

3 Answers3

0

It may be that the server is not returning a 200. Maybe it's returning a 304 not modified or a temporary redirect.

See Difference between .success() and .complete()?

.success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine.

However, .complete() will always get called no matter if the ajax call was suc

Community
  • 1
  • 1
FlavorScape
  • 13,301
  • 12
  • 75
  • 117
0

I think I know what it is. The ajax call is async, right? So it will take a period of time to get response. However if this call is generated via a link or form button and you don't return false here, then the normal get or post will occur. Just return false after calling $.ajax();

$.ajax({
                type: "POST",
                url: webMethod,
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (obj) {
                    alert("Success");
                    parseStart(obj.d, id, idnum, parseType);
                },
                error: function (e) {
                    alert("Failure. :" + e);
                }
            });
return false;
ek_ny
  • 10,153
  • 6
  • 47
  • 60
  • Tried this and no luck either. – Bill Blankenship May 21 '12 at 16:47
  • have you tried using firebug in order to see what's happening on server? what if you put an alert before the $.ajax() method.. would that get called? – ek_ny May 21 '12 at 16:59
  • Yeah, I did this I get an alert there. I tried IE step through and that is how I figured out that if I step through the entirety of the ajax call that it will sometimes work. It really makes no sense.... – Bill Blankenship May 21 '12 at 17:01
  • hmm.. when it doesn't work, what are the values for webMethod, or parameters? – ek_ny May 21 '12 at 17:05
  • It is the same as when it did work. The parameters are just a bunch of integers. The webmethod is just the name of my webmethod these are basically just constants at this point. – Bill Blankenship May 21 '12 at 17:07
  • how is the $.ajax called? what is it wired up to, if anything? – ek_ny May 21 '12 at 17:37
  • It is called via a button click. (onclientclick event) – Bill Blankenship May 21 '12 at 17:42
  • hmmm.. on a button click you probably don't even need to return false-- so I'm kind of stumped here. I think I would use firebug and see if my ajax call was being made to the server. My first guess was that it was redirecting before the ajax success call, but I might be wrong. – ek_ny May 21 '12 at 17:54
0

Ok, after long long troubleshooting on this. I switched the button call to the body onload versus a click of the button upon doing this it works without error. This was due to me using an asp button that was producing a call back on click breaking my post. I then removed my asp buttons and replaced these with an input button.

Bill Blankenship
  • 3,316
  • 6
  • 43
  • 73