在document.ready上调用javascript

时间:2012-08-06 16:29:50

标签: javascript jquery

我的jquery不会在文档准备好的情况下运行我的java脚本函数。

cont += "<script>";
cont += "$(document).ready(function() {Puma.getReasonForBTI()});";
cont += "</script>";

JS功能

Puma.getReasonForBTI = function() {
    var reason = document.getElementById("reasonId").value;
    var msql = "SELECT Pid FROM tPid WHERE Reason = 'reason'";
    sql = "action=getReasonForBTI&sql=" + encodeURIComponent(msql);
    objAjaxAd.main_flag = "getReasonForBTI";
    objAjaxAd.SendQuery(sql);
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

为什么不直接将DocReady添加到JS?

Puma.getReasonForBTI = function() {
    var reason = document.getElementById("reasonId").value;
    var msql = "SELECT Pid FROM tPid WHERE Reason = 'reason'";
    sql = "action=getReasonForBTI&sql=" + encodeURIComponent(msql);
    objAjaxAd.main_flag = "getReasonForBTI";
    objAjaxAd.SendQuery(sql);
}

$(document).ready(function() {
  Puma.getReasonForBTI()
});

修改

另外,我会自己发送reason并在服务器端清理它,然后将其放入查询中。通过Javascript / AJAX发送SQL查询只是在寻找麻烦。

仿皮代码:

sql("
    SELECT Pid
    FROM tPid 
    WHERE Reason = ?
", $ajax.reason)

DOUBLE EDIT

此外,将reason放在字符串中的单引号中不会评估reason的值。刚想我会为你节省一些未来的头痛

var foo = "bar";
console.log("The value of foo is 'foo'");
=> "The value of foo is 'foo'"
console.log("The value of foo is " + foo);
=> "The value of foo is bar"

答案 1 :(得分:1)

尝试使用Chrome浏览器和开发工具(F12)。

  1. 查看错误控制台。
  2. 修正错误
  3. 更改您的代码,因为有人可以使用您的代码删除基础数据库中的任何数据
  4. <强>更新

    var reason = document.getElementById("reasonId").value;
    // reason is entered directly byy a user (or Mr. EvilHacker).
    var msql = "SELECT Pid FROM tPid WHERE Reason = 'reason'";
    // Here you create a SQL, which may sounds like this:
    
    SELECT Pid FROM tPid WHERE Reason = ''; DROP table tPid;--'
    

    如果恶意黑客将';DROP table tPid;--输入文本框。请查看owasp.org以获取更多信息