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'
          });
        }
    
      });
    
    评论

报告相同问题?