0

I have got a problem about a ajax call function .I can't handle success or error result. This function get CalcPremium parameter as 100 .It should be change depend on ajax response ,but function again return 100. Ajax calling url is ok ,i don't understand what is problem. Here is my function :

  function get_result_by_code(CalcPremium) {
var retValue = CalcPremium
var promocode = parseFloat($('#bakcelPromo').val())
var url = '/docflow/' + project_id + '/' + document_id + '/promo/extract_json/'
$.ajax({
method: "POST",
url: url,
async: true,
data: { promocode: promocode },
success: function (response) {
    if (response.status == 1) {
           retValue = retValue * 0.5 }
         else {
           retValue = retValue * 2.0 }
    $('#output').html(response.message);
    },
    error: function () {
       retValue = retValue * 4.0
    }
    });
    return  retValue;
    } 
Avión
  • 7,963
  • 11
  • 64
  • 105
  • Do you get anything in your django console when you fire the request? And what does your Dev tools say about the response? – N. Ivanov Aug 04 '17 at 08:01
  • In console this error occured **UnboundLocalError at /docflow/2/8470/promo/extract_json/** **local variable 'result' referenced before assignment** – Vugar Shikhaliyev Aug 04 '17 at 08:11
  • is this `result` variable inside `CalcPremium`? – N. Ivanov Aug 04 '17 at 08:14
  • Yes , result is json dict. as **result = dict(status = status,message='Result Succesfully')** And i use this result status inside succes function as **if (response.status == 1....** – Vugar Shikhaliyev Aug 04 '17 at 08:43
  • Ajax called python function return result as : **{{status:1 ,message:'Result Succesfully'}}** – Vugar Shikhaliyev Aug 04 '17 at 08:45
  • eh since ajax is ASYNC, it is quite possible that you are not returning the correct `retVal` because it gets returned before the ajax is finished. Also I would check whats happening inside the `CalcPremium` function because you are calling it before result is assigned. – N. Ivanov Aug 04 '17 at 08:50
  • Should retVal returned where in **get_result_by_code** function ? – Vugar Shikhaliyev Aug 04 '17 at 08:58
  • I am not quite sure what you asked me, but if you want to return the `retVal` after the ajax is finished, you should do it inside both `success` and `error` instead of after the ajax. Hope this helps! – N. Ivanov Aug 04 '17 at 09:04
  • Yes i want it ,but as i know ,retVal can't use as **get_result_by_code** function result if i return it success or error – Vugar Shikhaliyev Aug 04 '17 at 10:28
  • Problem is on **method: "POST"** , for this need token .İ removed and it worked ) – Vugar Shikhaliyev Aug 04 '17 at 12:11

0 Answers0