Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建Javascript库仅用于较少的重写目的?_Javascript_Jquery_Ajax - Fatal编程技术网

创建Javascript库仅用于较少的重写目的?

创建Javascript库仅用于较少的重写目的?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我如何创建一个简单的javascript库,而不是为了一直编写函数 例如,对于Jquery的ajax,我必须使用以下内容: $.ajax({ url: xxx, type: xxx, dataType: 'json', data: xxx, success : function(data){ } }) 我希望能够像getajax(url,type,success function)或post

我如何创建一个简单的javascript库,而不是为了一直编写函数

例如,对于Jquery的ajax,我必须使用以下内容:

$.ajax({ url: xxx,
         type: xxx,
         dataType: 'json',
         data: xxx,
         success : function(data){
         }
       })
我希望能够像getajax(url,type,success function)或postajax(url,type,data,success function)这样做

这可能吗?我现在有两个三个问题 1.创建库似乎有很多工作要做?(我是新来的,难道我不能把它们打包,放在.js中导入吗?) 2.如何传递jQueryAjax在成功时所做的功能? 3.我可以在图书馆中包括图书馆吗

非常感谢,我是Javascript新手,有很多类似的网站需要基于相同的格式完成


正如我在评论中提到的,jQuery已经为此提供了两种方法

$.post(URL,data,callback);
$.get(URL,callback);
并回答关于扩展jQuery以获得更多功能的第二个问题

$.extend({
    myPlugin: function (someVar) {
        // do something here, in this case we'll write to the console
        console.log(someVar);
    }
});

$.myPlugin("Some Text");

我只想回来回答我自己的问题,因为我没有得到我以前寻求的帮助。使用jQueryPostandGetAjax函数,它不会处理错误。因此,只需创建另一个.js文件,添加以下内容并将其包含在html中

function getAjax(PageName, Action, Variables, dofunction) {

    $.ajax({
            url : PageName+'/'+Action+'?'+Variables,
            type : "GET",
            dataType : "json",
            success : dofunction,        
            error : function (xhr, ajaxOptions, thrownError) {
                    errLogin(xhr, ajaxOptions, thrownError);
                }
    });

}
函数postAjax(页面名、动作、变量、数据、dofunction){


}

Jquery$.post()和$.get()可以这样做。是的,你说得对,要使它在不同的浏览器上工作,需要做很多工作。Jquery Ajax只是一个例子,如果有一个函数我要使用很多次,我可以将它打包到库中并导入它吗?谢谢
    $.ajax({
            url : PageName+'/'+Action+'?'+Variables,
            type : "POST",
            dataType : "json",
            data : Data,
            success : dofunction,        
            error : function (xhr, ajaxOptions, thrownError) {
                    errLogin(xhr, ajaxOptions, thrownError);
                }
    });