2012-06-01 68 views
0

我用javaScript编写了一些编码。JavaScript:意外数据错误

function showPopupSettings(userName) 
     { 
      var jsonData; 
      setLoading("Loading User Settings"); 
      jsonData = new function(){ 
        this.className = "User"; 
        this.methodName = "showPopupSettings"; 
        this.userName = userName ; 
      } 
       data = JSON.stringify(jsonData); 
       $.post('ajaxpage.php',{ submit: "commonAction",data:data }, 
            function(data) { 
             removeLoading(); 
             $("#dialog") 
              .dialog("destroy") 
              .html(data) 
              .show() 
              .dialog({ 
               modal: true, 
               title: "User Settings", 
               buttons: { 
                Cancel: function() { 
                 $(this).dialog('close'); 
                }, 

                Password: function() { 
                 showChangePasswordTable(); 
                }, 

                Save: function() { 
                 saveNewUserSettings(); 
                 $(this).dialog('close'); 
                } 
               } 
              }); 
            }); 
     } 

通过使用JSLint的错误free.while我是用的JSLint检查会显示以下错误 线10:意外数据

如何纠正这个错误...

回答

1

你得到这个错误在这条线:

data = JSON.stringify(jsonData); 

它是由前行造成的,其应以分号结束,但不会:

jsonData = new function(){ 
    this.className = "User"; 
    this.methodName = "showPopupSettings"; 
    this.userName = userName ; 
}; //Semi-colon here! 

您的代码有许多其他问题。在上面的代码片段,你不希望new关键字:

jsonData = function() { //No 'new'! 
    this.className = "User"; 
    this.methodName = "showPopupSettings"; 
    this.userName = userName ; 
}; 

您所使用data不首先宣布它的data = JSON.stringify(jsonData)线,所以它泄漏到了全球范围。在使用之前,请使用var声明声明data

当我通过JSLint运行代码时,其他大部分错误都与空白有关,因此可以忽略。

+0

另外,jsonData可以立即声明和分配 –

+0

@Just_Mad - 它可以,但是然后JSLint会再次抱怨并告诉你将声明和前一个声明结合起来。 –