0

I have a jquery mobile site with the form submiting ajax Post request - and the callback is allways error not success - PLEASE see the url: property I think there is the problem:

HTML

    <div data-role="content">
        <form id="callAjaxForm">
            <div data-role="fieldcontain">
                <label for="firstName">First Name</label>
                <input type="text" name="firstName" id="firstName" value=""  />

                <label for="lastName">Last Name</label>
                <input type="text" name="lastName" id="lastName" value=""  />
                <h3 id="notification"></h3>
                <button data-theme="b" id="submit" type="submit">Submit</button>
            </div>
        </form>
    </div>

JS

    $(document).ready(function() {
        $("#submit").click(function(){

            var formData = $("#callAjaxForm").serialize();

            $.ajax({
                type: "POST",
                url: "http://localhost/wodowod/ajaxtest.php",
                cache: false,
                data: formData,
                success: onSuccess,
                error: onError
            });

            return false;
        });
    });

PHP

if(isset($_POST['firstName']) && isset($_POST['lastName'])){
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

echo("First Name: " . $firstName . " Last Name: " . $lastName);
}
    else {
    echo "Invalid Request";
}
ArloGrant
  • 249
  • 5
  • 15
  • have a look at this http://stackoverflow.com/questions/14468659/jquery-mobile-document-ready-vs-page-events – Vamsi Krishna B Jul 07 '13 at 15:21
  • cannot find anything useful - the posted code snippet is from a tutorial so I suppose it is correct, except that I cannot reach my php script when clicking on submit. – ArloGrant Jul 07 '13 at 15:27
  • Is it Same Domain or root folder?... if yes why dont u try first without the `http://localhost`... try first something like:::: if in same directory: `./ajaxtest.php`, else `pathToPhpFile/ajaxtest.php` – ErickBest Jul 07 '13 at 15:33
  • Well I tried it ...it works fine when I type it in firefox etc...but here it allways calls onError() method ...probably it can reach the script but never with success: onSuccess() – ArloGrant Jul 07 '13 at 15:41
  • The error hander is fired if the server side returned a code in the 500 range, so your PHP is probably producing a 500 internal server error. This is usually caused by a fatal error, such as a syntax error in the PHP code. Use Firebug or Chrome Dev Tools to watch the ajax call and see what the response is, then check your PHP error log to see what the problem is. – MrCode Jul 07 '13 at 15:59
  • the response is `200 OK` ...but firebug shows also `Ajax submission error` – ArloGrant Jul 07 '13 at 16:14
  • in chrome javascript tool - the console says: XMLHttpRequest cannot load http://localhost/wodowod/ajaxtest.php. Origin null is not allowed by Access-Control-Allow-Origin. – ArloGrant Jul 07 '13 at 16:17

1 Answers1

0

Just for the record - I found the solution at XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

If you're trying to do a cross-domain XMLHttpRequest via CORS...

Make sure you're testing via http://. Scripts running via file:// have limited support for CORS.
Community
  • 1
  • 1
ArloGrant
  • 249
  • 5
  • 15