javascript - Ajax Call with JS from Python -
i have simple django project name ajax .and installed 1 app project name promocode . problem in project ajax call between js , python. can't handle succes ajax call function . error: function () result got. know mistake in urls.py in promocode app , url mapping in ajax call function can't solve this. please me :)
my main urls.py file :
from datetime import datetime django.conf.urls import url,include import django.contrib.auth.views urlpatterns = [ url(r'^', include('promocode.urls'))] my promocode app urls.py file :
from django.conf.urls import url, include . import views urlpatterns = [ url(r'^$', views.index,name='index'), url(r'^$', views.extract_json)] my views.py file :
from django.shortcuts import render import json suds.client import client client 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 index.html file :
<!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 error communicating server.'); } }); document.getelementbyid("premium").value = parseint(sum_insured); } </script> </body> </html>
my main urls.py file :
from datetime import datetime 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 :
from django.conf.urls import url, include . import views urlpatterns = [ url(r'^$', views.index,name='index'), url(r'^promo/$', views.extract_json, name='extract_json')] my views.py file :
from django.shortcuts import render import json suds.client import client client 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 index.html file :
<!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 error communicating server.'); } }); document.getelementbyid("premium").value = parseint(sum_insured); } </script> </body> </html>
Comments
Post a Comment