weixin_33701564 2017-07-28 16:16 采纳率: 0%
浏览 74

从Python用JS进行Ajax调用

I have simple django project name Ajax .And i installed only one app for this project name Promocode . My problem in this project is Ajax call between js and python. I can't handle Succes with ajax call function . Error: function () result i got. I know i mistake in urls.py in promocode app and url mapping in ajax call function but i can't solve this. Please help me :)

My main urls.py file is :

from datetime import datetime
from django.conf.urls import url,include
import django.contrib.auth.views

urlpatterns = [
   url(r'^', include('promocode.urls'))]

My promocode app urls.py file is :

from django.conf.urls import url, include
from . import views

urlpatterns = [
       url(r'^$', views.index,name='index'),
       url(r'^$', views.extract_json)]

My views.py file is :

from django.shortcuts import render
import json
from suds.client import Client as Client
from django.http.response import HttpResponse


def index(request):
    return render(request,'promocode/index.html')


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='testaede35740f2b9d2248b0ab6b878',
         identicalCode=promocode,
         terminalCode=2148)
    if str(result[2]) == "SUCCESS":
        status = 1
    else:
        status = 0
    return status


def extract_json(request):
    data = json.loads(request.body)
    status = get_result_by_code(data['4545'])
    result = dict(
       status=status,
       message='Result Succesfully'
    )
    return HttpResponse(json.dumps(result), mimetype='application/json')

And my index.html file is :

<!DOCTYPE html>
<html>
<head>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>
<body>

<h3>Promocode Test </h3>

<label for="bakcelPromo">Promocode</label> <br>
<input type="text" name="bakcelPromo" id="bakcelPromo" class="form-control">
<br>
<br>
<label for="sum">Sum Insured</label> <br>
<input type="text" name="sum" id="sum" value="" class="form-control">

<p></p>

<button onclick="get_result_by_code();">Hesabla</button>

<br>
<br>
<label for="premium">Premium</label> <br>
<input id="premium" type="text" />
<br>
<br>

<script>
function get_result_by_code() {
var sum_insured = document.getElementById("sum").value * 0.035;
var promocode = document.getElementById("bakcelPromo").value;

$.ajax({
    type: "GET",
    url: "promocode/" ,
    dataType: "json",
    async: true,
    data: { "promocode": promocode },
    success: function (response) {
        if (response.status == 1) {
            sum_insured = sum_insured * 0.8

        } else {
            sum_insured = sum_insured * 1.5
            }
        $('#output').html(response.message);

        },
        error: function () {
            alert('There was an error communicating with the server.');
        }
});
document.getElementById("premium").value = parseInt(sum_insured);
}  
</script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33699914 2017-07-28 20:12
    关注

    My main urls.py file is :

    from datetime import datetime
    from django.conf.urls import url,include
    import django.contrib.auth.views
    
    urlpatterns = [
       url(r'^', include('promocode.urls'), namespace='appname')]
    

    My promocode app urls.py file is :

    from django.conf.urls import url, include
    from . import views
    
    urlpatterns = [
           url(r'^$', views.index,name='index'),
           url(r'^promo/$', views.extract_json, name='extract_json')]
    

    My views.py file is :

    from django.shortcuts import render
    import json
    from suds.client import Client as Client
    from django.http.response import HttpResponse
    
    
    def index(request):
        return render(request,'promocode/index.html')
    
    
    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='testaede35740f2b9d2248b0ab6b878',
             identicalCode=promocode,
             terminalCode=2148)
        if str(result[2]) == "SUCCESS":
            status = 1
        else:
            status = 0
        return status
    
    
    def extract_json(request):
        if request.is_ajax():
            promocode = request.POST.get("promocode")
            status = get_result_by_code(promocode)
            result = dict(
               status=status,
               message='Result Succesfully'
            )
            return HttpResponse(json.dumps(result), mimetype='application/json')
    

    And my index.html file is :

    <!DOCTYPE html>
    <html>
    <head>
    <script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
    </head>
    <body>
    
    <h3>Promocode Test </h3>
    
    <label for="bakcelPromo">Promocode</label> <br>
    <input type="text" name="bakcelPromo" id="bakcelPromo" class="form-control">
    <br>
    <br>
    <label for="sum">Sum Insured</label> <br>
    <input type="text" name="sum" id="sum" value="" class="form-control">
    
    <p></p>
    
    <button onclick="get_result_by_code();">Hesabla</button>
    
    <br>
    <br>
    <label for="premium">Premium</label> <br>
    <input id="premium" type="text" />
    <br>
    <br>
    
    <script>
    function get_result_by_code() {
    var sum_insured = document.getElementById("sum").value * 0.035;
    var promocode = document.getElementById("bakcelPromo").value;
    
    $.ajax({
        method: "POST",
        url: "{% url "appname:extract_json" %}" ,
        async: true,
        data: { "promocode": promocode },
        success: function (response) {
            if (response.status == 1) {
                sum_insured = sum_insured * 0.8
    
            } else {
                sum_insured = sum_insured * 1.5
                }
            $('#output').html(response.message);
    
            },
            error: function () {
                alert('There was an error communicating with the server.');
            }
    });
    document.getElementById("premium").value = parseInt(sum_insured);
    }  
    </script>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。