gpt4 book ai didi

javascript - AJAX和MYSQL,AJAX太快mysql查询不出来?

转载 作者:行者123 更新时间:2023-11-29 23:51:20 25 4
gpt4 key购买 nike

我正在开发一个 php/mysql 应用程序,我试图使用 window.onerror 将 javascript 错误收集到数据库中,在该函数内,我向 php 脚本发出 ajax 请求,该脚本会将错误记录到数据库中。然而,当我测试它时,应该记录了 13 个错误,但只有一个插入到数据库中。所有 13 个 ajax 请求都返回 200 OK,发生这种情况是因为 ajax 对于 mysql 查询来说太快了,无法处理任何内容。我尝试在发送请求上使用设置超时,但它似乎不起作用。

这是我的代码:

window.onerror = function(msg, url, line)
{

function createXHR()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}

return null;
}

function sendRequest(url, payload)
{
var xhr = createXHR();
if (xhr)
{
xhr.open("POST",url,true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
console.log(xhr.responseText);
}
};
xhr.send(payload);
}

}

function encodeValue(val)
{
var encodedVal;
if (!encodeURIComponent)
{
encodedVal = escape(val);
/* fix the omissions */
encodedVal = encodedVal.replace(/@/g, '%40');
encodedVal = encodedVal.replace(/\//g, '%2F');
encodedVal = encodedVal.replace(/\+/g, '%2B');
}
else
{
encodedVal = encodeURIComponent(val);
/* fix the omissions */
encodedVal = encodedVal.replace(/~/g, '%7E');
encodedVal = encodedVal.replace(/!/g, '%21');
encodedVal = encodedVal.replace(/\(/g, '%28');
encodedVal = encodedVal.replace(/\)/g, '%29');
encodedVal = encodedVal.replace(/'/g, '%27');
}
/* clean up the spaces and return */
return encodedVal.replace(/\%20/g,'+');
}

if (window.XMLHttpRequest) {

var master = "llesmana@ucsd.edu";

var payload = "msg=" + encodeValue(msg) + '&url=' + encodeValue(url) + "&line=" + encodeValue(line) + "&master=" + encodeValue(master);
var url_req = "http://104.131.199.129:83/php/log_error.php";
sendRequest(url_req, payload);
return true;
}

return false;

}

PHP:

<?php
/**
* Created by PhpStorm.
* User: xxvii27
* Date: 9/2/14
* Time: 12:30 PM
*/


/* Helper functions */
function gpc($name)
{
if (isset($_GET[$name]))
return $_GET[$name];
else if (isset($_POST[$name]))
return $_POST[$name];
else if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
else
return "";
}

//Database Connection
function connectDB (){
define('DB_HOST', 'localhost');
define('DB_NAME', 'userinfo');
define('DB_USER','root');
define('DB_PASSWORD','ohanajumba');

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD, DB_NAME) or die("Failed to connect to MySQL: " . mysql_error() );

return $con;
}


function logError($occured, $name, $line, $master, $url, $db){

$command="INSERT INTO errors (id, occured, name, url, line, master) VALUES (NULL, '$occured', '$name', '$url','$line', '$master')";
mysqli_query($db, $command) or die(mysql_error());
}


$db = connectDB();

$message = htmlentities(substr(urldecode(gpc("msg")),0,1024));
$url = htmlentities(substr(urldecode(gpc("url")),0,1024));
$line = htmlentities(substr(urldecode(gpc("line")),0,1024));
$master = htmlentities(substr(urldecode(gpc("master")),0,1024));

$date = date('Y-m-d G:i:s', time());

logError($date, $message, $line, $master, $url, $db);


mysqli_close($db);

此外,我已经检查了通过请求发送的所有数据,并且所有这些数据都已被脚本正确接收,任何帮助将不胜感激。

最佳答案

我解决了它,显然我忘记使用mysqli_real_escape_string()。

关于javascript - AJAX和MYSQL,AJAX太快mysql查询不出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com