> Hi Guys
>
> I am trying to make an ajax POST request but its not working. Code
> below:
>
> var _HOMEDIR = "http://localhost/personal/index.php/home/";
>
> $(document).ready(function() {
> $("form").submit(function() {
> var str = $("form").serialize();
> $.ajax({
> type: "POST",
> async: false,
> url: _HOMEDIR+"send?"+str,
> dataType: "json",
> beforeSend: function() {
> $("span:last").text("Sending..").show();
> },
> error: function() {
> $("span:last").text("Failed. Please try again").show();
> },
> success: function(data) {
> if(data.type == "failed") {
> $("span:last").text("Please enter valid data for each field").show
> ();
> } else {
> $("span:last").text("Thank you for your message").show();
> }
> }
> });
>
> return false;
> });
> });
>
Why not using malsup's form plugins:
http://malsup.com/jquery/form
also, after read
http://funkatron.com/site/comments/safely-parsing-json-in-javascript/
I don't use dataType: 'json' anymore. I use JSON.parse(), so I can make
sure that I parse the correct json response.
--
Donny Kurnia
http://blog.abifathir.com
http://hantulab.blogspot.com
http://www.plurk.com/user/donnykurnia
No comments:
Post a Comment