Solved

what's wrong with simple ajax call?

Posted on 2013-01-25
2
299 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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 …

738 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