2015-10-14 102 views
2

我想获得此URL的响应。但是当我检查控制台出现错误信息时:未捕获的类型错误:响应不是函数

Uncaught TypeError: response is not a function

可能出现什么问题?

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

这是我的功能。

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

这是我的回应:

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"}}) 
+1

检查这一项http://stackoverflow.com/questions/32450690/show-some-error-uncaught-referenceerror – guradio

+0

我已经声明'jsonpCallback得到了回报数据:'响应','在我的代码上。 – uno

+0

只需提一下,'success:function(json){callback(json);如果我使用这个'success:callback(json)'而不是这个'success:function(json){callback(json);},则可以简单地使用'success:callback(json)' –

回答

2

因为它是写here

sonpCallback 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.

通过设置你的Ajax对象的 jsonpCallback财产

所以,你传递一个函数的名字(或从jQuery 1.5的函数本身)应该被视为回调。这意味着如果您将其值设置为'response',则应声明response()函数。

0

工作例如:

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

设置一个demo here

0

只是使用这种类型的编码返回回调的数据。

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

现在你的console.log

相关问题