【问题标题】:Exploit JSON values coming from a get mapping利用来自 get 映射的 JSON 值
【发布时间】:2019-07-31 04:16:11
【问题描述】:

我试图通过集成在订单控制器中的 Web 服务在我的数据库中查找订单,如下所示:

@GetMapping("/view" )
@ResponseBody
public Optional<Ordre> view(String id) {

    return ordreRepository.findById(id) ;

}

然后使用下面的 javascript 代码以模式显示订单的详细信息:

$('.view').on('click', function(e) {
  e.preventDefault();

  var href2 = $(this).attr('href2');
  $.get(href2, function(ordre, status) {
    $('#Modalview #num_ord').val(ordre.num_ord);
    $('#Modalview #dte_ord').val(ordre.dte_ord);
    $('#Modalview #dir_ord').val(ordre.dir_ord);
    $('#Modalview #ref_ord').val(ordre.ref_ord);
    $('#Modalview #pap_ord').val(ordre.pap_ord);
    $('#Modalview #boss_ord').val(ordre.boss_ord);
    $('#Modalview #suj_ord').val(ordre.suj_ord);
  });

  $('#Modalview').modal();
});
<div class="modal fade" id="Modalview" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="padding-left: 10px;">&times;</button>

      </div>
      <div class="modal-body" style="padding-bottom: 50px;">

        <form class="form-horizontal" id="formview">

          <div class="form-group">

            <div class="col-sm-10">
              <input type="text" class="form-control pull-right" id="num_ord" name="num_ord" />
            </div>
          </div>

          <div class="form-group">

            <div class="col-sm-10">
              <input class="form-control pull-right" type="date" id="dte_ord" name="dte_ord" />
            </div>
          </div>

          <div class="form-group">

            <div class="col-sm-10">
              <input type="text" class="form-control pull-right" id="dir_ord" name="dir_ord" />
            </div>
          </div>

          <div class="form-group">

            <div class="col-sm-10">
              <input type="text" class="form-control pull-right" id="ref_ord" name="ref_ord" />
            </div>
          </div>

          <div class="form-inline">
            <div class="form-group pull-right">
            </div>
          </div>

          <br/>
          <br/>

          <div class="form-group " id="pap">

            <div class="col-sm-6 col-sm-push-3">
              <select class="form-control" id="pap_ord">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
              </select>
            </div>
          </div>
          <br/>
          <div class="form-group" id="boss">

            <div class="col-sm-8 col-sm-push-1">
              <input class="form-control pull-right" type="text" id="boss_ord" name="boss_ord" />
            </div>
          </div>

          <div class="form-group" id="suj" style="padding-top: 10px;">

            <div class="col-sm-10">
              <textarea class="form-control pull-right" id="suj_ord" name="suj_ord"></textarea>
            </div>
          </div>

          <div class="modal-footer" style="margin-right: -15px;">
            <button type="button" class="btn btn-default" data-dismiss="modal">Return Back</button>
          </div>
        </form>
        <!--/modal-body-collapse -->

        <!--/modal-footer-collapse -->

      </div>

    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>

但我不知道模式中没有显示任何内容,并且我的编辑器的控制台中没有错误或崩溃。任何人都可以帮助我,谢谢。

【问题讨论】:

    标签: javascript spring model-view-controller spring-data-jpa thymeleaf


    【解决方案1】:

    尝试如下所示,并使用th:fieldth:value 绑定您的表单对象

    $('.view').on('click', function(e) {
      e.preventDefault();
    
      $.ajax({
                type: 'get',
                 url: /*[[ @{'/view'} ]]*/,
    
                  success: function(returnedData){
                  $('#formview').html(returnedData)
                     $('#Modalview').modal('show'); 
    
    
                    },    
    
                    error: function(xhr, exception) {
                        console.log(xhr);
    
    
                        } 
            }); 
    
    
    });
    

    【讨论】:

    • 这在浏览器中给了我一个 json 响应,但没有显示在模态中
    • 在浏览器控制台中也会出现此错误 [ SyntaxError: expected expression, got ',' ] 在您指示 URL 为空的行中。
    • 这里的url不为空。这是您使用 thymeleaf 时编写 url 的方式。但是在脚本标签内你必须提到它。喜欢这个
    • [ SyntaxError: 预期表达式,得到 ',' ]
    • 它在说要做什么。
    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多