javascript - Uncaught TypeError: response is not a function - Stack Overflow

I am trying to get the response of this url. But when I check the console error message appears:Uncaugh

I am trying to get the response of this url. But when I check the console error message appears:

Uncaught TypeError: response is not a function

What is the possible problem?

var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';
getJsonData(uri, function(res){
});

This is my function.

var getJsonData = function(uri,callback){
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        url: uri,
        jsonpCallback: 'response',
        cache: false,
        contentType: "application/json",
        success: function(json){
            callback(json);
        }
    });
}

this is my response:

response({"_id":"56177d3b3f2dc8146bd8565c","event":"ConfbridgeJoin","channel":"SIP/192.168.236.15-0000005e","uniqueid":"1444379955.224","conference":"0090000293","calleridnum":"0090000290","calleridname":"0090000290","__v":0,"status":false,"sipSetting":{"accountcode":"0302150000","accountcode_naisen":"203","extentype":0,"extenrealname":"UID3","name":"0090000290","secret":"Myojyo42_f","username":"0090000290","context":"innercall_xdigit","gid":101,"cid":"0090000018"}})

I am trying to get the response of this url. But when I check the console error message appears:

Uncaught TypeError: response is not a function

What is the possible problem?

var uri = pbxApi+"/conference/participants/"+circle+"/"+data.conference+"/"+data.uniqueid+'?jsonp=response';
getJsonData(uri, function(res){
});

This is my function.

var getJsonData = function(uri,callback){
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        url: uri,
        jsonpCallback: 'response',
        cache: false,
        contentType: "application/json",
        success: function(json){
            callback(json);
        }
    });
}

this is my response:

response({"_id":"56177d3b3f2dc8146bd8565c","event":"ConfbridgeJoin","channel":"SIP/192.168.236.15-0000005e","uniqueid":"1444379955.224","conference":"0090000293","calleridnum":"0090000290","calleridname":"0090000290","__v":0,"status":false,"sipSetting":{"accountcode":"0302150000","accountcode_naisen":"203","extentype":0,"extenrealname":"UID3","name":"0090000290","secret":"Myojyo42_f","username":"0090000290","context":"innercall_xdigit","gid":101,"cid":"0090000018"}})
Share Improve this question edited Oct 14, 2015 at 7:42 Alexandr Lazarev 12.9k4 gold badges39 silver badges47 bronze badges asked Oct 14, 2015 at 5:24 unouno 8672 gold badges16 silver badges30 bronze badges 4
  • 1 check this one stackoverflow./questions/32450690/… – guradio Commented Oct 14, 2015 at 5:26
  • i already declared jsonpCallback: 'response', on my code. – uno Commented Oct 14, 2015 at 5:30
  • Just to mention, success: function(json){ callback(json); } is renundat, you can simply use success: callback(json) – Alexandr Lazarev Commented Oct 14, 2015 at 5:53
  • if i use this success: callback(json) instead of this success: function(json){ callback(json); } json will be undefine – uno Commented Oct 14, 2015 at 6:24
Add a ment  | 

3 Answers 3

Reset to default 1

As it is written here:

jsonpCallback Type: String or Function() Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

So by setting jsonpCallback property of your ajax object, you are passing a name of a function (or a function itself from jQuery 1.5) that should be treated as a callback. That means that if you set its value to 'response', a response() function should be declared.

Working example:

// first declare you function
function response(obj){console.log(obj)};  

$.ajax({ 
  url:'http://www.mocky.io/v2/561dedcb1000002811f142e5', 
  dataType:'jsonp', 
  jsonp:false, // make it to false, to use your function on JSON RESPONSE
  jsonpCallback: 'response',
  success: function(ret){
    console.log('ok');
  }
});

setup a demo here

just use this type of coding for return your callback data.

 function getJsonData(uri,callback)
  {
     $.ajax({
       type: "GET",
       dataType: "json",
       url: uri,
       cache: false,
       success: function(json){
         callback(json);
                }
      });
  }
      getJsonData(function(resp)
         {
          console.log(resp);
         }

Now you got the return data in console.log

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742397396a4436224.html

相关推荐

  • javascript - Uncaught TypeError: response is not a function - Stack Overflow

    I am trying to get the response of this url. But when I check the console error message appears:Uncaugh

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信