Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

what's wrong with simple ajax call?

Posted on 2013-01-25
2
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

722 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