Ajax“尝试加载资源时发生错误” Safari

时间:2018-10-23 19:06:12

标签: javascript php jquery ajax

我正在尝试通过Ajax将数据传递到userData.php,但无法弄清楚为什么我得到以下错误“尝试加载资源时发生错误”。

这是js代码:

myApp.onPageInit('form-elements', function(page) {

    $$('.page[data-page=form-elements] form[name=form]').on('submit', function(e) {
    e.preventDefault();
    var formData = myApp.formToData(e.target);
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "//autoapp.webxdetroit.com/material/assets/custom/php/userData.php",
            // url: "assets/custom/php/userData.php",
            data: {userData : formData },
            headers: {
                "cache-control": "no-cache"
            },
            success: function( data ) {
                window.location.href='assets/custom/php/camera.php'
        },
            error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }

});

    });

});

这是php:

  <?php

session_start();

require_once "pdo.php";
require_once "util.php";

unset($_SESSION['user_id']); //To log the last user out
unset($_SESSION['employee_id']); //To log the last user out

//  Handle the incoming data
if ( isset($_POST['userData'])) {
   $msg = validateProfile();
   if( is_string($msg)){
     $_SESSION['error'] = $msg;
     header("Location: userData.php");
     return;
   }

// Set user data variable
$userData = $_POST['userData'];

$stmt = $pdo->prepare('INSERT INTO User (name, email, phone, vin, mileage, employeeid)
   VALUES(:nm, :em, :ph, :vin, :mi, :eid)');

 $stmt -> execute(array(
   ':nm' => $userData['input_name'],
   ':em' => $userData['input_email'],
   ':ph' => $userData['input_phone'],
   ':vin' => $userData['vin'],
   ':mi' => $userData['mileage'],
   ':eid' => '1')
 );

 $row = $pdo->lastInsertId();
 echo($row);


 // Set session user_id
 $_SESSION['user_id'] = $row;


 // redirect the browser to index.php
 $_SESSION['success'] = "User Added";
 header("Location: userData.php");
}

?>


<!DOCTYPE html>
<html lang=“en”>
 <head>
   <meta charset=“UTF-8”>
   <title>Alex Lennard's Profile Add</title>
 </head>

 <body>
 <!-- Print flash message -->
   <?php
   flashMessages();
   ?>
 </body>
</html>

这是util.php:

<?php

function flashMessages(){
  if ( isset($_SESSION['error'])){
    echo('<p style="color:red;">'.htmlentities($_SESSION['error'])."</p>\n");
    unset($_SESSION['error']);
  }
  if ( isset($_SESSION['success'])){
    echo('<p style="color:green;">'.htmlentities($_SESSION['success'])."</p>\n");
    unset($_SESSION['success']);
  }
}


function validateProfile(){

  $userData = $_POST['userData'];

  if ( strlen($userData['input_name']) == 0 || strlen($userData['input_email']) == 0  ||
    strlen($userData['input_phone']) == 0  || strlen($userData['vin']) == 0  ||
    strlen($userData['mileage']) == 0 ){
    return "All fields are required";
  }
  if ( strpos($userData['input_email'], '@') === false){
    return "Email address must contain @";
  }
  return true;
}

?>

此代码在Google chrome(桌面)上可以正常运行,但在Safari(桌面/移动设备)或chrome(移动版)中不起作用。

如果我在Safari中提交空白表格,那么我的数据库中将提交空白表格,但仍会出现上述错误。当我尝试填充字段时,将一无所获。

Screenshot 1

Screenshot 2

0 个答案:

没有答案