Solved

what's wrong with simple ajax call?

Posted on 2013-01-25
2
295 Views
Last Modified: 2013-01-25
Hi,

I was just testing Google chrome debugger and I have this simple code to test an ajax call but  the ajax call is returning an error. The textStatus just states "error" and I tried to look at the console and it's blank. The username is being passed and the json is returning from the called php file "test_ajax_call.php" but the I guess something is wrong with this part:

$(document).ready(function() {
    $('#submit_form').click(function() {
        submit(); //check if username/email exists already
    });

});

function submit() {
    var username = $("#username").val();

    $.ajax({
        type : 'POST',
        url:'test_ajax_call.php',
        dataType: 'json',
        data: {
            username : username
        },
        success: function(data) {
            $("div#ret_username").text(data.username);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus);
        }
    });
}

What's wrong with the code?
test.php
init.js
test-ajax-call.php
0
Comment
Question by:Victor Kimura
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
1 - rename test-ajax-call.php to test_ajax_call.php
2 - replace (or don't use a submit button) :
$('#submit_form').click(function() {
        submit(); //check if username/email exists already
    });

Open in new window

by :
$('#submit_form').click(function(evt) {
        evt.preventDefault();
        submit(); //check if username/email exists already
    });

Open in new window

3 - use http or https to test your page... but I think it's ok for this part
0
 

Author Comment

by:Victor Kimura
Comment Utility
Hi Leaking971,

Thank you! yeah, I changed the submit to a regular button.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now