简体   繁体   English

如何使用Ajax在javascript文件上调用特定函数

[英]How to call a specific function on a javascript file with Ajax

I have file called functionContainer.js and in that file I have written 我有一个名为functionContainer.js文件,在该文件中我编写了

function returnTen(){
return 10;
}

function returnName(){
return "Stack Overflow";
}

and in callAjax.js file I need to call the returnTen function and get the data via an Ajax call. callAjax.js文件中,我需要调用returnTen函数并通过Ajax调用获取数据。

(functionContainer.js is hosted in a server) (functionContainer.js托管在服务器中)

How can I do this? 我怎样才能做到这一点?

If you want to execute returnTen() ; 如果要执行returnTen() after your ajax request finishes, then put your returnTen() function inside of a success, failure, or callback function ; 在您的ajax请求完成之后,然后将您的returnTen()函数放入成功,失败或回调函数中 whichever one you choose is up to you... 您选择的任一个取决于您...

Here some example with ExtJs Ajaxcall : 这是ExtJs Ajaxcall的一些示例:

Ext.Ajax.request({ Ext.Ajax.request({

url: 'page.php',

params: {
    id: 1
},
// scope: this, 

// you can set the scope too //您也可以设置范围

callback: function(opt,success,respon){

    **returnTen();**
} 

}); });

To make this method ( returnTen() ) callable from the client side script we need to make this static and decorate/adorn it with a WebMethod attribute. 为了使此方法( returnTen() )可从客户端脚本中调用,我们需要将此方法设为静态并用WebMethod属性装饰/修饰它。

The first thing that we need to do to be able to call the server side methods is to make the EnablePageMethods="true" in the ScriptManager control. 为了能够调用服务器端方法,我们需要做的第一件事就是在ScriptManager控件中使EnablePageMethods =“ true”。

In Your script you can call server side js function like this: 在您的脚本中,您可以像这样调用服务器端js函数:

function CallServerMethod() { 函数CallServerMethod(){

// call the server side method here
PageMethods.returnTen();       

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM