Javascript 方法不允许使用Ajax
我正在用jQuery学习AJAX,但我遇到了这个问题,我的页面上说Javascript 方法不允许使用Ajax,javascript,jquery,Javascript,Jquery,我正在用jQuery学习AJAX,但我遇到了这个问题,我的页面上说jQuery-3.6.0.js:10109 POSThttp://127.0.0.1:5500/data/addToCart.json 405(不允许使用方法)。我的页面有addtocart按钮,我想打印我在json文件中设置的价格,但出现了405错误。这是我第一次看到这个问题,我真的不知道如何解决它。这是我的密码: function addItem(id,name, description, price, moreInfo) {
jQuery-3.6.0.js:10109 POSThttp://127.0.0.1:5500/data/addToCart.json 405(不允许使用方法)
。我的页面有addtocart
按钮,我想打印我在json
文件中设置的价格,但出现了405错误。这是我第一次看到这个问题,我真的不知道如何解决它。这是我的密码:
function addItem(id,name, description, price, moreInfo) {
$('#input-new-item').val('')
let html = '';
html+='<div class="item" data-id="'+id+'">'
html+='<div class="name">'+name+'</div>'
html+='<img src="assets/python.png" alt="picture">'
html+='<div class="description">'+description+'</div>'
html+='<div class="price">'+price+'</div>'
html += '<button class="item-add">Add to cart</button>'
html += '<button class="item-remove">Remove</button>'
html+='<a class="more-info-link" href="#">More info</a>'
html+='<div class="more-info">'+moreInfo+'</div>'
html += '</div>'
$('#container').prepend(html);
}
$(document).ready(function () {
$.ajax('data/item.json', {
type: "get",
dataType: 'json',
contentType: 'application/json',
cache: false
})
.done(function (response) {
let items = response.items
items.forEach(item => {
addItem(item.id,item.name, item.description, item.price, item.moreInfo)
});
})
.fail(function (request, errorType, errorMessage) {
})
$('#container').on('click', '.item-add', function () {
let id = $(this).parent().data('id')
$.ajax('data/addToCart.json', {
type: "post",
data:{id: id },
dataType:'json',
contentType: 'application/json'
})
.done(function (response) {
if (response.message == 'Successfully added') {
console.log(response.price)
}
})
})
});
函数附加项(id、名称、说明、价格、更多信息){
$(“#输入新项”).val(“”)
让html='';
html+=''
html+=''+名称+''
html+=''
html+=''+描述+''
html+=''+价格+''
html+=“添加到购物车”
html+=“删除”
html+=''
html+=''+moreInfo+''
html+=''
$(“#容器”).prepend(html);
}
$(文档).ready(函数(){
$.ajax('data/item.json'{
键入:“获取”,
数据类型:“json”,
contentType:'应用程序/json',
缓存:false
})
.完成(功能(响应){
让items=response.items
items.forEach(item=>{
addItem(item.id、item.name、item.description、item.price、item.moreInfo)
});
})
.失败(功能(请求、错误类型、错误消息){
})
$(“#容器”)。在('单击','上。项添加',函数(){
让id=$(this.parent().data('id'))
$.ajax('data/addToCart.json'{
类型:“post”,
数据:{id:id},
数据类型:'json',
contentType:'应用程序/json'
})
.完成(功能(响应){
如果(response.message==“已成功添加”){
console.log(response.price)
}
})
})
});
addToCart.json
不支持post方法。这是json文件。@Vel,我看了教程,它工作得很好,但我的却不行:(((您想将项目添加到json文件?以及您使用的是哪种语言PHP或?@Vel,所以基本上我的操作与教程完全相同,他们使用post方法,然后打印json文件中的内容。@Vel,这是一门关于Udemy的课程,很难参考,我已经告诉过您视频的具体内容了。)