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>