2017-08-02 73 views
0

三个问题: 1)有什么办法让代码更整洁吗?主要是为了避免在报价问题中嵌套和引用这么多? 2)任何关于下面的解析错误的建议。我分别测试了一些代码以确保它可以正常工作。 (见下面的第二个代码框)。 3)当我查看处理此请求的NodeJS服务器程序中的request.body时,我得到“未定义”。从我读的内容来看,如果content-type被设置为application/json,我不需要body解析器。Dojo - 按钮点击request.post - 语法错误

<button id="updateStudentButton" data-dojo-type="dijit/form/Button" 
     data-dojo-props="iconClass:'dijitIconTask', 
      onClick:function(){ 
       var url = '../student/' + dom.byId('studentId').value; 
       var studId = dojo.byId('studentId').value; 
       dom.byId('studentFeedback').value += 
        'updateStudentButton clicked studentID=' + studId + '\n'; 
       var firstname = dom.byId('studentFirstname').value; 
       var lastname = dom.byId('studentLastname').value; 

       var postBody = JSON.parse(
          '{ \"data\": {' + 
          ' \"firstname\": "' + firstname + '\",' + 
          ' \"lastname\": "'+ lastname + '\"' + 
          '},' + 
          '\"headers\": { ' + 
          ' \"Content-Type\": \"application/json\" ' + 
          '}}' 
          ); 
       dom.byId('studentFeedback').value += 'postBody=' + postBody + '\n'; 
       require(['dojo/request'], function(request){ 
        // AJAX Post the data to the server 
        request.post(url, postBody 
        ).then(function(response){ 
         dom.byId('studentFeedback').value += response + '\n'; 
         // parse and return data in text boxes 
         var respJSON = JSON.parse(response); 
         var rowsAffected = respJSON.rowsAffected; 
         dom.byId('studentFeedback').value += 'rowsAffected=' + rowsAffected + '\n'; 
         }, 
         function(error){ 
          dom.byId('studentFeedback').value += response; 
         }); 
       }) 

      } 
      "> 
      Update 
</button> 

我在测试的NodeJS separatley,以确保所有的报价工作,并看到了正确的console.log:

   var firstname = 'John'; 
       var lastname = 'Doe'; 

       var postBody = JSON.parse(
          '{ \"data\": {' + 
          ' \"firstname\": "' + firstname + '\",' + 
          ' \"lastname\": "'+ lastname + '\"' + 
          '},' + 
          '\"headers\": { ' + 
          ' \"Content-Type\": \"application/json\" ' + 
          '}}' 
          ); 
console.log ("postBody="); 
console.dir (postBody); 

获得解析错误:

dojo/parser::parse() error Error: SyntaxError: Invalid or unexpected token in data-dojo-props='iconClass:'dijitIconTask', 
        onClick:function(){ 
         var url = '../student/' + dom.byId('studentId').value; 
         var firstname = dom.byId('studentFirstname').value; 
         var lastname = dom.byId('studentLastname').value; 

         var postBody = JSON.parse(
            '{ \' 
    at Object.construct (parser.js.uncompressed.js:401) 
    at Object.<anonymous> (parser.js.uncompressed.js:190) 
    at Object.map (dojo.js:8) 
    at Object._instantiate (parser.js.uncompressed.js:184) 
    at parser.js.uncompressed.js:893 
    at _2f8 (dojo.js:8) 
    at Promise.then._305.then (dojo.js:8) 
    at Object.parse (parser.js.uncompressed.js:890) 
    at Object._parse (html.js.uncompressed.js:301) 
    at Object.onEnd (html.js.uncompressed 

回答

1

避免引号问题中引号的方式是有您的onClick javascript代码在<script></script>标签内。

在您的代码的修改版本下面,在我的环境中工作正常。该请求激活错误回调,因为我没有服务器处理端。

请参阅文档here以获取声明性详细信息(使用data-dojo事件)和here以获取请求详细信息。

对于请求,我已将数据字符串化,因为这是我在我的应用程序(服务器端的php)中执行的操作。文档说它可以是一个字符串或对象,你可能想要尝试发送数据对象,这取决于你的服务器环境的期望。

RGDS, JC

<!DOCTYPE HTML> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>Neal Walters stask overflow test</title> 
     <link rel="stylesheet" href="dojo-release-1.12.2-src/dijit/themes/claro/claro.css" media="screen"> 

    </head> 
    <body class="claro"> 
     <button type="button" id="updateStudentButton" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'dijitIconTask'"> 
      <span>update</span> 
      <script type='dojo/on' data-dojo-event='click'> 
        var dom = require('dojo/dom');     
        var url = '../student/' + dom.byId('studentId').value; 
        var studId = dom.byId('studentId').value; 
        dom.byId('studentFeedback').value += 'updateStudentButton clicked studentID=' + studId + '\n'; 
        var firstname = dom.byId('studentFirstname').value; 
        var lastname = dom.byId('studentLastname').value; 
        var data = {firstname: firstname, lastname: lastname}; 
        //dom.byId('studentFeedback').value += 'postBody=' + postBody + '\n'; 
        require(['dojo/request'], function(request){ 
        // AJAX Post the data to the server 
         request.post(url, {data: JSON.stringify(data), method: 'POST', handleAs: 'json'}).then(
          function(response){ 
           dom.byId('studentFeedback').value += JSON.stringify(response) + '\n'; 
           // parse and return data in text boxes 
           var rowsAffected = response.rowsAffected; 
           dom.byId('studentFeedback').value += 'rowsAffected=' + rowsAffected + '\n'; 
          }, 
          function(error){ 
           dom.byId('studentFeedback').value += error; 
          } 
         ); 
        }); 
      </script> 
     </button><p> 
     <form> 
      Student feedback: <input id="studentFeedback"><p> 
      Student first name: <input id="studentFirstname"><p> 
      Student last name: <input id="studentLastname"><p> 
      Student id: <input id="studentId"><p> 
     </form> 
     <script src="dojo-release-1.12.2-src/dojo/dojo.js" data-dojo-config="async:true"></script> 
     <script type="text/javascript"> 
      require(["dojo/parser", "dijit/form/Button", "dojo/domReady!"], 
      function(parser){ 
       parser.parse(); 
      }); 
     </script> 
    </body> 
</html> 
+0

谢谢!这是这么多更好,更容易,另外,在NotePad ++中,语法高亮现在已经出现,因为它现在全是一个大字符串,我不断从一堆示例中拉一些代码,最后以一个混乱结束,欢迎来到StackOverflow! – NealWalters

1
data-dojo-props="iconClass:'dijitIconTask' 

不你需要以“?

data-dojo-props="iconClass:'dijitIconTask'" 

可以更好的你正在创建JSON EX的方式:

var json={}; 
json.name="Test"; 
json.age="23" 
当您打印

JSON(JSON.stringify(json)),你会看到

{ 
    "name":"Test", 
    "age":"23" 
    } 
+0

我有“>的按钮,这是‘更新’的JSON。 大点尖端的文本前右,会尽力的! – NealWalters