Search code examples
javascriptajaxmethodscall

Javascript AJAX method


why does this not work?

    function AjaxCall (FormName, PHPFunction) {
    alert(FormName);
    $.ajax({
        type: "GET",
        url: "webservice.php?method=" + PHPFunction,
        data: $("'" + FormName + "'").serialize(),
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert(response);
        }
    }); 
   }

and this is the call from the form:

<form id="form_login" name="form_login" method="POST" onsubmit="return AjaxCall('form_login','CheckUserLogin')">

Thank you


Solution

  • FormName will be 'form_login', which you use as a jQuery selector which would match <form_login> elements if you weren't adding quotes to the string, but simply isn't a valid selector as you are.

    Don't mess around with passing identifying strings, just pass the element itself.

    onsubmit="return AjaxCall(this, ...
    

    and

    $(FormName)