A
A
Alexander Arbuzov2017-09-29 14:26:57
JavaScript
Alexander Arbuzov, 2017-09-29 14:26:57

How to get the result of a hook through ajax?

Tell me, how can I make it so that I can pull some getHook.php file through ajax and get the result of Hook::exec(<hook name>) in response?
I want to update the payment delivery methods available to him via ajax after the user is authorized in the basket (via ajax), etc. information.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Fomin, 2019-06-19
@narem

The very first line you declare the variable "a". As I understand it, you want to use it in your condition. If so, then consider the following code:

$("#text-two").css("background",function(a){
    if(a == 1){
      return "red";
    }else{
      return "gray";
    }
  });

The IF block is in the context of the callback function that you pass as an argument to the css function. Let's take a closer look at this callback function:
function(a){
    if(a == 1){
      return "red";
    }else{
      return "gray";
    }
  }

In javascript, there is such a thing as variable scope. When you create a function, a LexicalEnvironment object is created along with it, in your case it will look like this {a: undefined}. When you access a variable inside a function, the first thing you do is access that object. That is, your access to the variable "a" will look like LexicalEnvironment.a, in your case it will not be a unit, but undefined. That is why the condition is not met. In order to refer to an external variable, you need to remove the variable "a" from the arguments of your callback function.
$("#text-two").css("background",function(){
    if(a == 1){
      return "red";
    }else{
      return "gray";
    }
  });

Now, when the function does not find a variable in the LexicalEnvironment, it will refer to the external variable object, which is where the variable "a" with the value "1" is located.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question