views:

420

answers:

4

I have a registration form and am using [jQuery][1] [Ajax][2] to submit it.

Here is the jQuery Ajax code:

$(document).ready(function() {
    $("form#regist").submit(function() {
        var str = $("#regist").serialize();
        $.ajax({
            type: 'POST',
            url: 'submit1.php',
            data: $("#regist").serialize(),
            dataType: 'json',
            success: function() {
                $("#loading").append("<h2>you are here</h2>");
            }        
        });
        return false;        
    });
});

I have validation at submit1.php for email and username. When I submit the form with valid data it enters the database and if I enter some repeated data or invalid email it does not enter the database. So my submit1.php works fine.

My main problem is that the success function event at Ajax does not work.

**

Removing dataType:json did work and my success function event was called. However now i got another issue. In my submit1.php file i check for repeated email address , and username in db. If the email or username is repeated then an error needs to be generated to the user without page refresh. I have put a die() in my server script. how do i return this response at success event in ajax?? thanks..

**

A: 

The success callback takes two arguments:

success: function (data, textStatus) { }

Also make sure that the submit1.php sets the proper content-type header: application/json

Darin Dimitrov
Thanks for your reply. I read about this on jquery ajax documentaion but i cannot figure out what my next step to should be to get it working... would appreciate if you could point me to the right direction. Also does it mean that i should have some specific code in my php file?? thanks a lot
noobcode
the fact that `success` takes two arguments seems completely irrelevant to the question. You can pass any amount of parameters to a javascript function, regardless of how many it accepts in its declaration, so that's definitely not the cause of these issues, and since none of the values `data` or `textStatus` are being used in the success callback, there seems to be no good reason to declare them in the function at all.
David Hedlund
And JSON data doesn't have to have correct content-type header, it just has to be in JSON format.
Tatu Ulmanen
A: 

Put an alert() in your success callback to make sure it's being called at all.

If it's not, that's simply because the request wasn't successful at all, even though you manage to hit the server. Reasonable causes could be that a timeout expires, or something in your php code throws an exception.

Install the firebug addon for firefox, if you haven't already, and inspect the AJAX callback. You'll be able to see the response, and whether or not it receives a successful (200 OK) response. You can also put another alert() in the complete callback, which should definitely be invoked.

David Hedlund
Thanks for your reply. Tried alert in success event but it did not work. I have firebug addon for firefox and it does receive a successful ( 200 OK ) response so no issues there.. I have a die function called in my php file for any errors and when i check it in firebug , it shows proper response. what do you mean by "You can also put another alert() in the complete callback, which should definitely be invoked."??Thanks a lot for a quick reply
noobcode
if you don't see the alert in your `success`, then it is not a success. since the response is 200 OK, Tatu's reply seems reasonable, but for further troubleshooting, you can use another event, called `complete` which is always invoked, regardless of whether or not a request is successful (`success` only happens if the request is successful). `complete: function (xhr, status) { alert('complete: '+status); }`
David Hedlund
+2  A: 

The result is probably not in JSON format, so when jQuery tries to parse it as such, it fails. You can catch the error with error: callback function.

You don't seem to need JSON in that function anyways, so you can also take out the dataType: 'json' row.

Tatu Ulmanen
+1 seems like a probable cause of the error
David Hedlund
yes..!! thanks a lot tatu Ulmanen... removing dataType : 'json' did work out and now i get the desired result..I really appreciate your reply to my query..Thanks all..thanks
noobcode
@idrish, it would be polite to mark the correct answer as accepted so others won't accidentally answer anymore.
Tatu Ulmanen
A: 

Although the problem is already solved i add this in the hope it will help others.

I made the mistake an tried to use a function directly like this (success: OnSuccess(productID)). But you have to pass an anonymous function first:

  function callWebService(cartObject) {

    $.ajax({
      type: "POST",
      url: "http://localhost/AspNetWebService.asmx/YourMethodName",
      data: cartObject,
      contentType: "application/x-www-form-urlencoded",
      dataType: "html",
      success: function () {
        OnSuccess(cartObject.productID)
      },
      error: function () {
        OnError(cartObject.productID)
      },
      complete: function () {
        // Handle the complete event
        alert("ajax completed " + cartObject.productID);
      }
    });  // end Ajax        
    return false;
  }

If you do not use an anonymous function as a wrapper OnSuccess is called even if the webservice returns an exception.

js newbee
Thanks @js newbee.
noobcode