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

tereško
  • 56,151
  • 24
  • 92
  • 147
Django Anonymous
  • 2,737
  • 15
  • 55
  • 98

2 Answers2

2

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

<button type="button" id="save" >Save</button>
daghan
  • 788
  • 7
  • 15
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.

Konsole
  • 3,389
  • 3
  • 24
  • 38
  • This thread has some good explanation on ajax status code zero http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – Konsole Jul 23 '13 at 07:48
  • OK on this note http://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` – Django Anonymous Jul 23 '13 at 07:51
  • 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. – Konsole Jul 23 '13 at 08:01
  • 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. – Konsole Jul 23 '13 at 08:06