Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

what's wrong with simple ajax call?

Posted on 2013-01-25
2
Medium Priority
?
302 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 2000 total points
ID: 38818639
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
ID: 38819590
Hi Leaking971,

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

971 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