4

I have a form and I want to add AJAX functionality to this form.

Architecture: MVC

I have a basic form like this:

<form id="customForm" method="post">
        <input type="text" id="name" name="zone_name" value="" class="input-block-level" />
        <button type="submit" id="save" >Save</button>
  </form>

I have this JS on my MVC-View:

$('#save').click(function()
{
    var name = $('#name').val();
    $.ajax
    ({
    type: 'POST',
    url: 'http://localhost/myApp/process',
        data: "{name:"+name+"}",
            success: function (result) {
                alert(result);
            },
            error: function () {  
            alert('fail');              
            }
        });
    });

I have a process class which is there in controller and have this code within

class process {
    function __construct() {
        echo 'Constructor';
    }
}

But Doing all this gives me Error message through AJAX. Why is this happening? Is there any other way of doing this. Here under is snapshot:

enter image description here

Here under is my .HTACCESS rule

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

So when I am directly navigation to my process class its working. But not with AJAX. What would be the possible reason for this?? Here under is the screenshot:

enter image description here

8
  • When you navigate to it you are using GET, then when you use Ajax you are using POST, is your controller RESTful? Do you define your routes somewhere and have to make them respond to more than one HTTP Verb? Commented Jul 23, 2013 at 7:34
  • No actually i am accessing it on a page called localhost/myApp/myPage And on there the Ajax is calling this process class... Commented Jul 23, 2013 at 7:36
  • 1. you don't need " here: "{name:"+name+"}", you need an object, not a string: data: {name: name}, Commented Jul 23, 2013 at 7:38
  • Which framework are you using? Commented Jul 23, 2013 at 7:38
  • 1
    @FDL what if he wants to send only name, and just didn't include other fields in his example to make it shorter? Commented Jul 23, 2013 at 7:45

2 Answers 2

2

your button must not submit the form, make its type button:

<button type="button" id="save" >Save</button>
Sign up to request clarification or add additional context in comments.

Comments

0

What is the ajax status code? Is it generating any error message? You can get the status code as:

$('#save').click(function(e)
{
    e.preventDefault(); //prevent default button action
    var name = $('#name').val();
    $.ajax
    ({
    type: 'POST',
    url: 'http://localhost/myApp/process',
        data: "{name:"+name+"}",
            success: function (result) {
                alert(result);
            },
            error: function (xhr, status, errorMessage) {  
             alert(xhr.status);              
            }
        });
    });

If the status code is zero the network access my be forbidden. I see localhost in your ajax url and 192.168.1.6 in your alert box.

4 Comments

This thread has some good explanation on ajax status code zero stackoverflow.com/questions/2000609/jquery-ajax-status-code-0
OK on this note stackoverflow.com/a/2016085/1182021 I don't think any XSS is happening and page does not exist as I am able to access the page through http://localhost/myApp/process
Check the ajax call in firebug. The firebug generally displays ajax error like 500 Internal Server Error and 403 Forbidden error. I think this function is not available in chrome element inspector.
I suspect that your ajax call is not being complete because the submit button reloads the page before getting the ajax response. Sorry for bad english.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.