我如何在ajax回调函数中调用javascript函数

时间:2020-11-29 21:12:46

how i call javascript function in ajax callback function and how i can pass arguments to this javascript function like field name or something

我如何在ajax回调函数中调用javascript函数以及如何将参数传递给此javascript函数,如字段名称或其他东西

'#ajax' => array(
  'callback' => 'ajax_javascript_function',
  'wrapper' => 'd-div-autocomplete-textfield-div',
  'method' => 'replace',
  'event' => 'blur',
  'effect' => 'fade',
  'progress' => array('type' => 'throbber', 'message' => ''),
),

1 个解决方案

#1


0  

You need to pass those variables to a server with javascript ajax JSONP. There are many ways, but here are 2 examples:

您需要使用javascript ajax JSONP将这些变量传递给服务器。有很多方法,但这里有两个例子:

With plain querystring:

使用简单的查询字符串:

$.ajax({
 type: "GET",
 dataType: "jsonp",
 url: "some.php?callback=mycallback",
 data: "name=John&location=Boston",
 success: function(response){
   alert( "Data received: " + received);
  },
error: function(e){
   alert(e);
}
});

with object for querystring parameters

用于查询字符串参数的对象

$.ajax({
   type: "GET",
   dataType: "jsonp",
   url: "some.php?callback=mycallback",
   data: {
      "name" : "John",
      "location" : "Boston"
    }
   success: function(response){
     alert( "Data received: " + response );
   },
    error: function(e){
     alert(e);
    }
 });

Your PHP code must output its response with the callback you asked for in this javascript (I used 'mycallback'). If you are not writing the PHP(or some kind of server side code) then that server must be agreeing to return responses wrapped with the callback function you asked for it to use. This way, the response gets into your javascript because you told it what function would be callable. This is called JSONP architecture. It works because the one thing that you can request Cross-Domain is a script.

您的PHP代码必须使用您在此javascript中要求的回调输出其响应(我使用了'mycallback')。如果您没有编写PHP(或某种服务器端代码),那么该服务器必须同意返回包含您要求其使用的回调函数的响应。这样,响应进入你的javascript,因为你告诉它什么功能可以调用。这称为JSONP架构。它的工作原理是因为您可以请求跨域的一件事是脚本。

PHP

PHP

echo "mycallback('" + $data + "');";

Good luck, read more here: http://api.jquery.com/jQuery.ajax/

祝你好运,在这里阅读更多内容:http://api.jquery.com/jQuery.ajax/

#1


0  

You need to pass those variables to a server with javascript ajax JSONP. There are many ways, but here are 2 examples:

您需要使用javascript ajax JSONP将这些变量传递给服务器。有很多方法,但这里有两个例子:

With plain querystring:

使用简单的查询字符串:

$.ajax({
 type: "GET",
 dataType: "jsonp",
 url: "some.php?callback=mycallback",
 data: "name=John&location=Boston",
 success: function(response){
   alert( "Data received: " + received);
  },
error: function(e){
   alert(e);
}
});

with object for querystring parameters

用于查询字符串参数的对象

$.ajax({
   type: "GET",
   dataType: "jsonp",
   url: "some.php?callback=mycallback",
   data: {
      "name" : "John",
      "location" : "Boston"
    }
   success: function(response){
     alert( "Data received: " + response );
   },
    error: function(e){
     alert(e);
    }
 });

Your PHP code must output its response with the callback you asked for in this javascript (I used 'mycallback'). If you are not writing the PHP(or some kind of server side code) then that server must be agreeing to return responses wrapped with the callback function you asked for it to use. This way, the response gets into your javascript because you told it what function would be callable. This is called JSONP architecture. It works because the one thing that you can request Cross-Domain is a script.

您的PHP代码必须使用您在此javascript中要求的回调输出其响应(我使用了'mycallback')。如果您没有编写PHP(或某种服务器端代码),那么该服务器必须同意返回包含您要求其使用的回调函数的响应。这样,响应进入你的javascript,因为你告诉它什么功能可以调用。这称为JSONP架构。它的工作原理是因为您可以请求跨域的一件事是脚本。

PHP

PHP

echo "mycallback('" + $data + "');";

Good luck, read more here: http://api.jquery.com/jQuery.ajax/

祝你好运,在这里阅读更多内容:http://api.jquery.com/jQuery.ajax/