weixin_33691598 2017-05-23 15:08 采纳率: 0%
浏览 29

访问外部ajax参数

I have a function with 4 parameter

function getToken(u, p, url, role) {
var that = this;
oururl = url;
$.ajax({
    type: 'GET',
    url: "/Base/getConfigUrl",
    success: function (data) {
        $.ajax({
            type: 'GET',
            async: false,
            url: url + '?username=' + u + '&password=' + p,
            success: 'callbackFunc',
            error : 'callbackError',
            contentType: "application/json",
            dataType: 'jsonp'
        });
    }

});

and the call back function

function callbackFunc(resultData) {
// how to get the outer parameter
}

inside call back function I need to access role parameter I logged the this variable and i can't find anything

  • 写回答

1条回答 默认 最新

  • weixin_33726943 2017-05-23 15:15
    关注

    You can't, per se. The role variable does not exist in the right scope.

    You need to refactor your code so that you can get the variable from somewhere that it does exist and pass it to the callbackFunc function.

    See the comments inline with the code for an explanation of all the changes.

    function callbackFunc(resultData, role) {
      // Edit the argument list above to accept the role as an additional argument
    }
    
    
    function getToken(u, p, url, role) {
      var that = this;
      var oururl = url; // Best practise: Make this a local variable. Avoid globals.
      $.ajax({
        type: 'GET',
        url: "/Base/getConfigUrl",
        success: function(data) {
          $.ajax({
            type: 'GET',
            // async: false, // Remove this. You are making a JSONP request. You can't make it synchronous. 
            url: url;
            data: { // Pass data using an object to let jQuery properly escape it. Don't mash it together into the URL with string concatenation. 
              username: u,
              password: p
            },
            success: function(data) { // Use a function, not a string
              callbackFunc(data, role); // Call your function here. Pass the role (and the data) as arguments.
            },
            error: function() {}, // Use a function, not a string
            // contentType: "application/json", // Remove this. You are making a JSONP request. You can't set a content type request header. There isn't a request body to describe the content type of anyway.
            dataType: 'jsonp'
          });
        }
    
      });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗