2

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)

2 Answers 2

1

It seems like your example is missing some important bits, like a Django view handler that returns a JSON response. The URL that you're using in the ajax call ("/docflow/projects/modules_2") - is that mapped to a view?

A quick example would be something like this:

# 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')

And then in terms of the javascript/frontend, that should look something like this:

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.');
    }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

0

I have to write in answer section, as I'm too low on point to make comments yet.

What does it mean "I know this function is wrong and need to fix". Doesn't it return any results?

Try to add error function callback:

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

Alternatively check what is in json parameter in success function (log it with console.log or with alert. Also check what is in browser console. Is there any error message?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.