2017-07-27 42 views
1

我需要了解從JS到Python的簡單Ajax調用。我有一個Python函數。該函數使用一個簡單的參數並返回結果。我想從js發送這個參數,並得到js的函數結果。我嘗試如下.python函數是可以的,但js方面我知道我犯了一些錯誤。這裏是我的python代碼,function.py:Django中使用Ajax與JS和Python之間的數據集成

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與promocode。

而我的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) 

回答

0

我在回答部分來寫,因爲我太低的點進行評論。

這是什麼意思「我知道這個功能是錯誤的,需要修復」。它不會返回任何結果嗎?

嘗試添加error回調函數:

$.ajax({ 
    ... 
    error: function (request, status, error) { 
     alert(request.responseText); 
    } 
}); 

或者檢查什麼是json參數success函數(console.logalert記錄它 還要檢查什麼是瀏覽器控制檯有任何。錯誤消息

0

看起來您的示例似乎缺少一些重要的位,例如返回JSON響應的Django視圖處理程序。在ajax調用(「/ docflow/projects/modules_2」) - 是映射到視圖?

一個簡單的例子是這樣的:

# 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.'); 
    } 
    }); 
}