【发布时间】:2021-04-09 03:26:53
【问题描述】:
不知何故,我的数据没有发布到 /api/recipe/recipes/ 我的 html
{% extends 'base.html' %}
{% block content %}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a Recipe</title>
</head>
<body class="">
<form>
{% csrf_token %}
<br>
<label for="T">Title: </label>
<input type="text" name="T" value="">
<br>
<label for="Ingr">Ingredients: </label>
<select class="" name="Ingr">
{% for ing in Ing %}
<option value="ing.pk">{{ing}}</option>
{%endfor%}
</select>
<br>
<label for="Tag">Tags: </label>
<select class="" name="Tag">
{% for tag in Tag %}
<option value="Tag.pk">{{tag}}</option>
{%endfor%}
</select>
<br>
<label for="Time">Time: </label>
<input type="text" name="Time" value="">
<br>
<label for="P">Price: </label>
<input type="text" name="P" value="">
<br>
<label for="link">Link: </label>
<input type="text" name="link" value="">
<br>
<input type="submit" name="submit" value="Post">
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$('form').on('submit', function(e) {
e.preventDefault()
var data = {
title: $('[name="T"]').val(),
tags: $('[name="Tag"]').val(),
ingredients: $('[name="Ingr"]').val(),
time_minutes: $('[name="Time"]').val(),
price: $('[name="P"]').val(),
link: $('[name="link"]').val()
};
var headers = {
'X-CSRFToken': '{{ csrf_token }}'
}; // Inject our token into the javascript using a template tag
$.ajax({
type: 'POST',
data: data,
headers: headers, // Set the headers in the request
url: '/api/recipe/recipes/',
success: function(res) {
console.log(res)
},
});
})
</script>
</body>
</html>
{%endblock%}
我正在尝试将此表单发布到上面 URL 中的其余框架中,如果您有任何解决方案,请帮助我提前感谢您
我在提交时在控制台中收到这些错误
Uncaught TypeError: $.ajax is not a function
at HTMLFormElement.<anonymous> ((index):101)
at HTMLFormElement.dispatch (jquery-3.1.1.min.js:3)
at HTMLFormElement.q.handle (jquery-3.1.1.min.js:3)
这 3 个错误对我来说是一场噩梦,我尝试了很多方法,但都没有奏效,我真的很感激任何帮助:D
【问题讨论】:
-
一个问题是您需要在此
<script src="https://code.jquery.com/jquery-3.1.1.min.js">之后关闭</script>标记 -
它没有解决我的错误我看到人们使用类似这样的东西 $(document).on("click",'.shares', function (event) { 这行得通吗?如果可以你在我的代码上做吗?谢谢,
-
如果您完全删除
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>会怎样?我想知道两次导入 jquery 是否会导致问题。 -
很遗憾,它没有用
-
那我很难过。如果我导入脚本和
console.log(typeof $.ajax),它会显示“函数”
标签: javascript html jquery ajax rest