2014-01-09 21 views
0

我正在使用Javascript類/對象來處理其中包含多個方法/函數的所有ajax請求。我一半在那裏,但我一直堅持與未捕獲的SyntaxError:在變量中聲明調用函數時發生意外標識符(並傳遞參數)

'Uncaught SyntaxError: Unexpected Identifier'

當我試圖調用一個參數命名的函數。我查看了周圍的stackoverflow,並沒有找到適合我的解決方案,這意味着我可能犯了一個我無法發現的錯誤。

的功能設計爲使用如下:

ajax.post(url, POST data, onLoad callback function); 
ajax.get(url, GET parameters, onLoad callback function); 
//example: 
ajax.get('http://api.domain.com/get', {x:1,y:2}, callback_function_when_complete); 

你的想法..和代碼:

ajax = { 
    xhr: (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(), 
    readyStateCode: null, 
    httpStatusCode: null, 
    response: null, 
    returnValue: function(onLoadCallbackFunction){ 
     switch (onLoadCallbackFunction, response) { 
      case (typeof(onLoadCallbackFunction) != 'undefined'): 
      case (onLoadCallbackFunction != null): 
       returnValue = response; 
       console.log('Ajax onLoadCallbackFunction: No onload callback function called, returning response to caller.'); 
       break; 

      case (onLoadCallbackFunction == false): 
       returnValue = true; 
       console.log('Ajax onLoadCallbackFunction: Caller doesn\'t want response.') 
       break; 

      case (typeof(onLoadCallbackFunction) == "function") 
       returnValue = onLoadCallbackFunction(response); 
       console.log('Ajax onLoadCallbackFunction: Function specified, function called with response in arguments.'); 
       break; 

      default: 
       returnValue = false; 
       console.log('Ajax onLoadCallbackFunction: Switch failed, returning false'); 
       break; 
     } 
     return returnValue; 
    }, 
    get: function(url, parameters, onLoadCallbackFunction){ 
     if (typeof parameters != 'undefined' && parameters != null && parameters != ''){ 
      var str = []; 
      for(var key in parameters) 
      if (parameters.hasOwnProperty(key)) { 
       str.push(encodeURIComponent(key) + "=" + encodeURIComponent(parameters[key])); 
      } 
      url = url + '?' + str.join("&"); 
     } 

     this.xhr.onreadystatechange = function(){ 
      this.readyStateCode = this.xhr.readyState; 
      this.httpStatusCode = this.xhr.status; 
     } 

     this.xhr.open('GET', url, true); 
     this.xhr.send(); 
     return this.returnValue(onLoadCallbackFunction, this.response); 
    }, 
    post: function(url, data, onLoadCallbackFunction, mimeType){ 
     //todo mimeType 
     this.xhr.onreadystatechange = function(){ 
      this.readyStateCode = this.xhr.readyState; 
      this.httpStatusCode = this.xhr.status; 
     } 

     this.xhr.open('POST', url, true); 
     this.xhr.send(data); 
     return this.returnValue(onLoadCallbackFunction, this.response); 
    }, 
    upload: function(url, file, onLoadCallbackFunction, onStateChangeCallbackFunction){ 
     //todo 
     //temporary 
     return false; 
    } 
    //todo onerror handler 
}; 

我就要拋出的錯誤在我的returnValue()函數在返回一個被調用的函數的時候會在開關櫃內運行(解析器甚至在運行之前也會標記它)。

case (typeof(onLoadCallbackFunction) == "function") 
    **returnValue = onLoadCallbackFunction(response);** 
    //Uncaught SyntaxError: Unexpected Identifier on line above 
    //... 
    break; 

,是的,我是完全正確的,現在卡住了,我知道我可以使用eval()但我真的不想這樣做。它拋出這個錯誤,並且沒有通過它傳遞參數。

對不起,我的代碼的混亂和長度,它仍然需要整理。 此外,我是一個新手,所以如果有人可以解釋我要去哪裏的錯誤,將非常感謝,在此先感謝。擔。

回答

1

你是你的case語句後缺少:

case (typeof(onLoadCallbackFunction) == "function"): 
// ------------------------------------------------^ 
+0

哦,親愛的我,總是有一些小的。非常感謝你haha –

+1

一般來說,請注意許多「意外的X」錯誤是由於它在丟失某些東西之前立即引起的); –

+0

它總是缺少換行符或未關閉的括號,這要歸功於:) –

相關問題