PHP和Ajax调用之间的ERR_EMPTY_RESPONSE错误

时间:2018-12-17 16:26:37

标签: javascript php jquery ajax

我阅读了很多与此类似的问题,但是在我阅读的线程中,对于这个问题确实没有答案。我对服务器脚本进行了简单的Ajax调用,以比较两个字符串。但是,尽管如此,但有时服务器会以jquery.min.js:4 POST https://www.example.com/otpcheck net::ERR_EMPTY_RESPONSE和类似的内容进行响应:XHR failed loading: POST

由于我实际上可以在Chrome调试器上确认发布数据,因此该错误的真正出处在哪里。这是我的Ajax调用脚本:

// --------------- START REGISTRATION OTP CHECK -----------------------------
$(document).ready(function() {

// Hide error label
	$("#error2").hide();
	$("#loader2").hide();
	 

	// process the form
	$('#RegistrationOTPForm').submit(function(event) {

		$("#error2").hide(); // hide errors when form submitted

		// get the form data
		// there are many ways to get this data using jQuery (you can use the class or id also)
		var formData = {
			'OTP' 				: $('input[name=OTP]').val()
		};

		// process the form
		$.ajax({
			type 		: 'POST', // define the type of HTTP verb we want to use (POST for our form)
			url 		: './OTPcheck.php', // the url where we want to POST
			data 		: formData, // our data object
			dataType 	: 'json', // what type of data do we expect back from the server
			beforeSend: function()
            {
                $("#error2").hide();
				$("#error3").hide();
				$("#loader").hide();
				$("#loader2").hide();
				$("input[type=submit]").attr("disabled", "disabled");
				$("input[type=text]").attr("disabled", "disabled");
				$("input[type=password]").attr("disabled", "disabled");
				$("select").attr("disabled", "disabled");
            },
			encode 		: true
		})
			// using the done promise callback
			.done(function(data) {

				// log data to the console so we can see
				console.log(data); 

				// here we will handle errors and validation messages
				if ( ! data.success) {
					
						// handle errors for  ---------------
	
	if (data.errors.OTP) {
		$("#OTP").val("");
		 $("input[type=submit]").removeAttr("disabled");
		 $("input[type=text]").removeAttr("disabled");
		 $("input[type=password]").removeAttr("disabled");
		 $("select").removeAttr("disabled");
		$("#error2").show(function(){
		$('#error2').html('' + data.errors.OTP + ''); // add the actual error message to the error label
		});

	}
				} else {

					// ALL GOOD! just show the success message!
					$("input[type=password]").attr("disabled", "disabled");
					$("input[type=submit]").attr("disabled", "disabled");
					$("input[type=text]").attr("disabled", "disabled");
					$("select").attr("disabled", "disabled");
                    window.location.href= "./RecoverUserIDSecurityInformation";
				}
			})

			// using the fail promise callback
			.fail(function(data) {

				// show any errors
				// best to remove for production
				console.log(data);
			});

		// stop the form from submitting the normal way and refreshing the page
		event.preventDefault();
	});

});

// --------------- END REGISTRATION OTP CHECK -----------------------------

这是它调用的服务器端脚本:OTPcheck.php

<?php
if(!isset($_SESSION)) 
    { 
        session_start(); 
    } 
//-- data and error arrays	
$errors         = array();  	// array to hold validation errors
$data 			= array(); 		// array to pass back data
// --------------------

$Real_OTP = "123456";
/**********************************************************************/

// -------- Function to test input
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
// -------------------------

if ( !empty($_POST)) {
	
		// Variables from post
	$GLOBALS['OTP'] = test_input($_POST["OTP"]); 
/*************************************************************/
// Checks for empty Amount
  if (empty($OTP)) {
   $errors['OTP'] = 'You have entered an invalid code';
  } else if (strcmp($OTP, $Real_OTP) !== 0){ //--Comparing both strings if they are not same while maintaning case sensitivity like Cow should be Cow so if == 0, strings are same. If !==0 strings are not equal
	  $errors['OTP'] = 'You have entered an invalid code';
  } else {
	  $_SESSION['RecoverUserIDProgress'] = "Progress";
	  
	  $data['success'] = true;	  
  } 
   
  
 /*************************************************************/  


/************************************************************/

	if ( ! empty($errors)) {
		// if there are items in our errors array, return those errors
		$data['success'] = false;
		$data['errors']  = $errors;
	} else {
	
	$data['success'] = true;

		// if there are no errors process our form, then return a message
		
		
	}
	
} else {
	$data['success'] = false;
		$data['errors']  = $errors;
}

// return all our data to an AJAX call
	echo json_encode($data);
?>

从上面可以看到,它是一个简单的字符串比较脚本。我的默认值为123456,我只需要比较从HTML表单发布的字段的值是否与我在服务器端脚本中设置的默认值匹配。为什么我会有这个net::ERR_EMPTY_RESPONSEXHR failed loading: POST

如果有人能指出我正确的道路,我将非常感激。因此我已经失去了睡眠。感谢您的所有答复和事先的支持。

0 个答案:

没有答案