Javascript AJAX没有传递硬编码数据

Javascript AJAX没有传递硬编码数据,javascript,python,json,django,ajax,Javascript,Python,Json,Django,Ajax,在index.js中,我有: $("#weather_form").on("submit", function(event){ event.preventDefault(); $.ajax({ url: "/weather/", type: "POST", data: {type_of_person: "1", exercis

index.js
中,我有:

 $("#weather_form").on("submit", function(event){
    event.preventDefault();

    $.ajax({
      url: "/weather/",
      type: "POST",
      data: {type_of_person: "1",
        exercise: "2",
        unit: "3",
        zip_postal: "4"},
      dataType: "json",
      contentType: "json",
      success: function (data){
        alert("success");
      },
      error: function(xhr,errmsg,err) {
        alert("errmsg: " + errmsg + "\nerr: " + err + "\nxhr.status: " + xhr.status + "\nxhr.responseText: " + xhr.responseText);
      }
    });
  });
我得到以下错误:

因此,我们知道由于弹出窗口,它将进入AJAX调用的错误函数。但是为什么呢

我专门硬编码了要传递的JSON值

处理AJAX数据的视图:

class weather(base.TemplateView):
    template_name = "weather/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["form"] = forms.input_form()
        return context

    @staticmethod
    def post(request, *args, **kwargs):
        form = forms.input_form(request.POST)

        if form.is_valid():
            # process the data
            type_of_person = form.cleaned_data["type_of_person"]
            exercise = form.cleaned_data["exercise"]
            unit = form.cleaned_data["unit"]
            zip_postal = form.cleaned_data["zip_postal"]

            results_matrix = interface.get_results_matrix(type_of_person, unit, exercise, zip_postal)
           
            return http.JsonResponse({"results_matrix": results_matrix.tolist()}, status=200)
        else:
            return http.JsonResponse({"error": form.errors}, status=400)
我尝试过的事情,但没有成功:

class weather(base.TemplateView):
    template_name = "weather/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["form"] = forms.input_form()
        return context

    @staticmethod
    def post(request, *args, **kwargs):
        form = forms.input_form(request.POST)

        if form.is_valid():
            # process the data
            type_of_person = form.cleaned_data["type_of_person"]
            exercise = form.cleaned_data["exercise"]
            unit = form.cleaned_data["unit"]
            zip_postal = form.cleaned_data["zip_postal"]

            results_matrix = interface.get_results_matrix(type_of_person, unit, exercise, zip_postal)
           
            return http.JsonResponse({"results_matrix": results_matrix.tolist()}, status=200)
        else:
            return http.JsonResponse({"error": form.errors}, status=400)
  • data:JSON.stringify({type_of_person:“1”,exercise:“2”,unit:“3”,zip_posal:“4”})

如果您从服务器收到400状态码,那么调用
success
是没有意义的

您的数据在前端有效并进入服务器,但未通过后端验证(或后端无法正确接受),因此它会返回错误的请求


在进行jQuery AJAX调用时,任何介于200-299之间且与304不同的错误代码都被视为错误。

我认为表单无法读取数据,因为您正在发送
json
contentType
。只要去掉那条线就行了。此外,您还必须将
csrf
标题添加到post请求中。因此:

$.ajax({
      url: "/weather/",
      type: "POST",
      data: {
        "csrfmiddlewaretoken": $('[name=csrfmiddlewaretoken]').val(),
        "type_of_person": "1",
        "exercise": "2",
        "unit": "3",
        "zip_postal": "4"
      },
      dataType: "json",
      // contentType: "json", remove this
      success: function (data){
        alert("success");
      },
      error: function(xhr,errmsg,err) {
        alert("errmsg: " + errmsg + "\nerr: " + err + "\nxhr.status: " + xhr.status + "\nxhr.responseText: " + xhr.responseText);
      }
    });

向我们显示您正在发送的视图data@BiploveLamichhane完成!我用正在处理AJAX数据的视图进行了更新。嘿,成功了!
dataType
contentType
之间有什么区别?那么,
contentType
是我们发送到服务器的数据,
dataType
是从服务器发送的数据类型或响应数据。有关更多详细信息,请参阅。