Forums

Data Integration Between JS and Python in Django with Ajax

Hello

I need to understand a simple call with ajax from JS to Python .I have a python function .This function take a simple parameter and return a result . I want to send this parameter from js ,and get function result to js . I try as below .Python function is ok ,but js side i know i made some wrongs . Here is my python code , 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

This function return 1 or 0 with promocode .

And my javascript function is below. I know this function is wrong and need to fix:

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);
    }
});
}

And last calculation function in js that will be played on screen is :

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)

What is it doing? What do you want it to do?

I just want send value from js to python function.py and get result from function.py into js.

And use returned value in IF operator for calculate one value .

I get that. What specifically is the problem? Is your Python not returning the right value? Is your Javascript not getting the URL? Not handling the value correctly?