2015-10-14 105 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

相關問題