【发布时间】:2023-03-17 02:53:02
【问题描述】:
我正在使用反映服务器端 Rails MVC 的 javascript 构建一个 MVC 框架客户端。我构建的系统进行频繁的 AJAX 调用并发送模型文件中定义的 jQuery 对象。
我遇到的问题是模型对象附加了方法,当 jQuery 通过 AJAX 发送它们时,它会执行对象中的每个方法。
谁能解释为什么会发生这种情况以及防止它的方法?
示例模型
var search = function (){
this.id = null;
this.investor = null;
this.program = null;
this.parent = null;
this.client = null;
this.date = null;
this.custom_order = "custom_order";
this.data_set = "all";
this.recursive = false;
this.search = null;
this.state_filter = PUB | DRAFT;
this.message = function()
{
alert("Message");
}
}
示例控制器
var nav_object = {search: System.search, page: System.page};
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/guidelines/search",
data: nav_object,
dataType: 'json',
success: function(data){
//Success
if(data.errors)
{
alert(data.errors.message);
}
else
{
System.guidelines.search = data.guidelines;
System.page = data.page;
System.meta.searchState = false;
loadView();
}
}
});
在上面的示例中,System.search 是搜索类的一个实例。当它被插入到 nav_object 并发送到服务器时,消息函数就会运行。
我知道我可以在发送之前取出类的成员并构建一个新对象我只是觉得有很多可以避免的代码。
【问题讨论】:
-
嗨,我不知道一种方法,但也许我得到了一些建议。您收到带有 GET 请求的程序代码。这是一个安全问题。想象一下一个坏人伪造该请求以访问敏感数据。干杯
-
感谢您的回复。在这种情况下,GET 正在提取首先需要身份验证且无法访问敏感信息的内部数据。在这种情况下使用 GET 是为了防止有人更改数据库信息。
标签: jquery ajax model-view-controller