Javascript对象在函数内调用函数

标签 javascript jquery ajax

我对用对象编写 javascript 很陌生。现在我尝试执行 ajaxcall 并拥有回调函数,但我可以在 ajax 调用中访问回调函数。

这是代码,我该如何调用回调函数?

function smarttalkObj() {

    this.base_url = 'http://';

    this.getPts = function() {

        $.ajax({
            url: this.base_url + 'getPTS',
            dataType: "jsonp",
            jsonp : "callback",
            jsonpCallback: "this.callback_pts",
            contentType: 'application/x-javascript'
        }); 
    }


    }

    this.callback_pts = function(data) {

        console.log(data);

    }


}

smarttalk = new smarttalkObj();

smarttalk.getPts();

最佳答案

ajax 方法可以接收成功错误回调函数作为设置的一部分。像这样的事情:

   $.ajax({
        url: this.base_url + 'getPTS',
        dataType: "jsonp",
        jsonp : "callback",
        contentType: 'application/x-javascript',
        success: function(data, textStatus, jqXHR){
            console.log(data);
            // your callback function here
        },
        error: function(jqXHR, textStatus, errorThrown){
            // callback in case of error
        }
    }); 

关于Javascript对象在函数内调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513062/

相关文章:

jquery - 使用 jQuery UI 使可拖动元素可在可放置元素中排序

Ajax跨域调用

javascript - 无法在 IE 11 中通过 FileReader 读取 SQL blob

javascript - 在 WebOS/Mojo 中从舞台助手调用 setupWidget

javascript - 服务器端验证内联显示成功或错误消息

javascript - jQuery 工具提示添加换行符

c# - ajax beginForm 忽略指定按钮

javascript - jQuery promise (当 > 然后)不使用 $.each 和 $.ajax 迭代数组

javascript - 当其他元素消失时动画/缓和元素定位

javascript - 测试 Angular Controller 定义如 angular.module ('myApp' ).controller(