javascript - Django 中 JS 和 Python 之间的数据集成与 Ajax

标签 javascript jquery python ajax django

我需要理解从 JS 到 Python 的 ajax 简单调用。我有一个 python 函数。这个函数接受一个简单的参数并返回一个结果。我想从js发送这个参数,并将函数结果获取到js。我尝试如下。Python 函数没问题,但 js 方面我知道我犯了一些错误。这是我的 python 代码,function.py:

from suds.client import Client as Client
def get_result_by_code(promocode):
url="http://service.emobile.az:8080/ws-loyalty-
program/cp/loyaltyprogram.wsdl"
client = Client(url)
result = client.service.loyaltyProgramCalculate(
       amount=1000,
       authKey='TEST6aede35740f2b9d2248b0ab6b878',
       identicalCode=promocode,
       terminalCode=2166)
if str(result[2])=="SUCCESS":
    status = 1
else:
    status = 0
return status

此函数返回 1 或 0 以及促销代码。

我的 javascript 函数如下。我知道这个函数是错误的,需要修复:

function get_result_by_code() {
promocode = $('#bakcelPromo').val();
  $.ajax({
  type: "GET",
  url: "\docflow\projects\modules_2",
  dataType: "json",
  async: true,
  data: {"promocode": promocode},
 succes: function (json) {
   $('#output').html(json.message);
}

}); }

最后在屏幕上播放的js计算函数是:

function calculate() {
   if ( get_result_by_code.val() == 1 )
      calculated_premium = calculated_premium * 0.2
   else  calculated_premium = calculated_premium
   calculated_premium = Math.ceil(calculated_premium)

最佳答案

您的示例似乎缺少一些重要的部分,例如返回 JSON 响应的 Django View 处理程序。您在 ajax 调用中使用的 URL(“/docflow/projects/modules_2”) - 是否映射到 View ?

一个简单的例子是这样的:

# urls.py
from django.conf.urls import patterns, url
from . import views

urlpatterns = patterns('',
    url(r'^/docflow/projects/modules_2$', views.docflow_projects_modules_2_view),
)

# views.py
import json
from suds.client import Client as Client
from django.http.response import HttpResponse


def get_result_by_code(promocode):
    url = "http://service.emobile.az:8080/ws-loyalty-program/cp/loyaltyprogram.wsdl"
    client = Client(url)
    result = client.service.loyaltyProgramCalculate(
        amount=1000,
        authKey='TEST6aede35740f2b9d2248b0ab6b878',
        identicalCode=promocode,
        terminalCode=2166)
    if str(result[2]) == "SUCCESS":
        status = 1
    else:
        status = 0
    return status


def docflow_projects_modules_2_view(request):
    data = json.loads(request.body)
    status = get_result_by_code(data['promocode'])

    result = dict(
        status=status,
        message='Put a message here....'
    )

    return HttpResponse(json.dumps(result), mimetype='application/json')

然后就 javascript/前端而言,应该如下所示:

function get_result_by_code() {
  var promocode = $('#bakcelPromo').val();
  $.ajax({
    type: "GET",
    url: "/docflow/projects/modules_2",
    dataType: "json",
    async: true,
    data: {"promocode": promocode},
    success: function (response) {
      if (response.status === 1) {
        // handle success
      } else {
        // handle error
      }
      $('#output').html(response.message);
    },
    error: function () {
       alert('There was an error communicating with the server.');
    }
  });
}

关于javascript - Django 中 JS 和 Python 之间的数据集成与 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351202/

相关文章:

jquery - 使用 Twitter Bootstrap 的响应式布局

c++ - 使用 SWIG 在 Python 中访问 C++ typedef

Python函数相乘时出现递归错误

javascript - 为什么 require ('../models/owners.js' ) 旁边还有另一个相邻的参数?

javascript - Ext Component 查询选择器格式和选项

javascript - Testcafe - 处理视频

javascript - 重构 jQuery 以影响 N 个 anchor 和 div 对的数量

javascript - 从 Bootstrap-Treeview 获取当前数据(对象/json)

jquery - 向上滑动 <p>

python - itertools.izip() 用于未预定义的列表计数