我想从jqueryajax转移到原生Javascript Ajax。这是我的jquery代码
下面是JavaScript代码 我已经更改了jQuery使用并转换为JavaScript的发送请求函数:我想从jqueryajax转移到原生Javascript Ajax。这是我的jquery代码,javascript,jquery,ajax,Javascript,Jquery,Ajax,下面是JavaScript代码 我已经更改了jQuery使用并转换为JavaScript的发送请求函数: var ajax, ajaxHandler; ajax.getUsernotification = { setup: function(command, data) { //retrieving the default attributes defined in ajaxhandler var attributes = ajaxHandler.defaultAttri
var ajax, ajaxHandler;
ajax.getUsernotification = {
setup: function(command, data) {
//retrieving the default attributes defined in ajaxhandler
var attributes = ajaxHandler.defaultAttributes;
//Setting the attribute according to requirement
attributes.url = 'index.php/request/' + command.toLowerCase();
attributes.data = data;
attributes.type = 'POST';
attributes.success = this.success,
attributes.error = (this.error) ? this.error : attributes.error,
//send request function defined in ajaxHandler below where
//we send updated attributes to perform ajax command
ajaxHandler.sendRequest(attributes);
},
success: function(data) {
//if success then here is my functionality to execute
Controllers.NotificationController.displayUserWebNotification(data);
}
}
//ajax Handler function through which I set default attribute and send request function defined to send request to server
ajaxHandler = {
defaultAttributes: {
type: 'GET',
url: 'index.php/request',
datatype: 'json',
data: {},
success: null,
error: function(data) {
errorHandler.showError('An Error occurred while trying to retreive your requested data, Please try again...');
},
timeout: function() {
errorHandler.showError('The request has been timed out, Please check your Internet connection and try again...');
}
},
sendRequest: function(attributes) {
//i perform here through jquery
$.ajax(attributes);
}
它在jQuery中工作正常,但在加载资源时出现错误:服务器在JavaScript中的响应状态为403禁止。我正在使用Ubuntu14.1,我想知道错误是与数据库相关还是不正确的代码。在jQuery中工作正常。这意味着什么?这意味着它给出了正确的结果,并且没有显示任何错误。但是在JavaScript中它不起作用。[…]我想知道错误是与数据库相关的还是不正确的代码。[…]当原始代码起作用,但不是您的实现时,很可能是因为不正确的代码。除此之外,您应该检查networking选项卡,并将jQuery完成的请求与您执行的请求进行比较。url是否相同,都发送相同的参数/数据,方法是否与您在jQuery中使用的POST相同,XMLHttpRequest是否默认使用GET,…我在attribute.type中设置了方法,然后在JS代码中传递了attribute.type。它告诉方法函数何时执行。
sendRequest: function(attributes) {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
attributes.success(attribute.data);
}
}
xmlhttp.open(attributes.type, attributes.url, true);
xmlhttp.send(attribute.data);
}