質問

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

役に立ちましたか?

解決

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)

他のヒント

This Code will work fine:

HTML Code:

<form id="form_login" name="form_login" method="POST" onsubmit="" action="">

E-Mail: <input type="text" size="30" name="email" id="email" >
Passwort: <input type="text" size="30" name="password" id="password" >
DeviceID: <input type="text" size="30" name="deviceid" id="deviceid" >

<input type="submit" value="Login" name="submit_login" />

</form>

JAVASCRIPT Code:

$(function () {
    $('#form_login').on('submit', function (e) {
        $.ajax({
            type: 'GET',
            url: 'webservice.php?method=CheckUserLogin',
            data: $('#form_login').serialize(),
            success: function (response) {
                alert(response);
            },
            failure: function (response) {
                alert(response);
            }
        });
        e.preventDefault();
    });
});

so, and now i change it to them:

HTML Code:

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

E-Mail: <input type="text" size="30" name="email" id="email" >
Passwort: <input type="text" size="30" name="password" id="password" >
DeviceID: <input type="text" size="30" name="deviceid" id="deviceid" >

<input type="submit" value="Login" name="submit_login" />

</form>

JAVASCRIPT Code:

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

i get no request to webserice.php and i don't know why. I can see any AJAX request, i use fiddle, all i can see is: "/projekte/werbung/login.php"

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top