Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将json响应重定向到Ajax并放入JSP中,由EL解析_Javascript_Jquery_Json_Ajax_Jsp - Fatal编程技术网

Javascript 将json响应重定向到Ajax并放入JSP中,由EL解析

Javascript 将json响应重定向到Ajax并放入JSP中,由EL解析,javascript,jquery,json,ajax,jsp,Javascript,Jquery,Json,Ajax,Jsp,首先介绍一下背景知识: (我是一名学生,请容忍我。) 使用jQuery,我制定了一个对RESTWeb服务的Ajax PUT请求 $.ajax({ type: "PUT", url: putURI, dataType: "json", cache: false, headers: { Accept : "application/json; charset=utf-8", "Content-

首先介绍一下背景知识: (我是一名学生,请容忍我。) 使用jQuery,我制定了一个对RESTWeb服务的Ajax PUT请求

$.ajax({
    type: "PUT",
    url: putURI,
    dataType: "json",
    cache: false,
    headers: {          
        Accept : "application/json; charset=utf-8",         
        "Content-Type": "application/json; charset=utf-8"   
    },
    success: function (){
        $.getJSON(baseURI, function(data){
             $.each(data, function (i, task) {
                 alert(task.description);
                 row = "<tr><td id='" + task.description + "'></td></tr>";
                 $("#tasksTableBody tr:last").after(row);
            });
        });
    },
    error: function (request, status, error) {
        alert(request.responseText);
        alert(status);
        alert(error);
    }
});
$.ajax({
键入:“放置”,
网址:putURI,
数据类型:“json”,
cache:false,
标题:{
接受:“application/json;charset=utf-8”,
“内容类型”:“应用程序/json;字符集=utf-8”
},
成功:函数(){
$.getJSON(baseURI,函数(数据){
$。每个(数据、功能(i、任务){
警报(任务描述);
行=”;
$(#tasksTableBody tr:last”)。在(第行)之后;
});
});
},
错误:功能(请求、状态、错误){
警报(request.responseText);
警报(状态);
警报(错误);
}
});
如果可能的话,我希望将响应重新路由到我的JSP,其中有EL和jstl,以便在我想要的地方创建表中的行

<tbody id="tasksTableBody">
   <core:forEach items="${allTasks}" var="task">
   <tr>
      <td id="desc${task.id}">
          ${task.description}
      </td>
      <f:parseDate value="${duedate}" pattern="yyyy-MM-dd" var="duedate" />
      <td id="due${task.id}" value=duedate>
          <f:parseDate value="${duedate}" pattern="yyyy-MM-dd"/>
      </td>
      <td id="done${task.id}" data-isDone="${task.isdone}">
          <core:set var="isdone" value="${task.isdone}"/>
          <core:if test="${isdone == true}">
             <img src="images/checked.png"/>
          </core:if>
      </td>
      <td>
         <img src="images/edit-icon.png" onclick="editItem(${task.id})" />
      </td>
      <td>
        <img src="images/delete.png" onclick="deleteItem(${task.id})" />
      </td>
    </tr>
  </core:forEach>
  </tbody>

${task.description}

这是可能的,还是我需要坚持使用“success”函数并尝试使其工作?

返回HTML而不是JSON是一种有效的方法,通常比在客户端处理JSON更简单

只需让您的控制器/servlet处理提交给JSP的内容,然后使用JQuery用返回的HTML更新某些元素。所以这看起来像:

$.ajax({
    type: "PUT",
    url: putURI,
    dataType: "json",
    cache: false,
    headers: {          
        Accept : "application/json; charset=utf-8",         
        "Content-Type": "application/json; charset=utf-8"   
    },
    success: function (html){
        $( "#results" ).append( html ); //append response to an element with id 'results'

        // or

        $( "#results" ).html( html ); //replace contents of element with id 'results'
    },
    error: function (request, status, error) {
        alert(request.responseText);
        alert(status);
        alert(error);
    }
});

返回HTML而不是JSON是一种有效的方法,通常比在客户端处理JSON更简单

只需让您的控制器/servlet处理提交给JSP的内容,然后使用JQuery用返回的HTML更新某些元素。所以这看起来像:

$.ajax({
    type: "PUT",
    url: putURI,
    dataType: "json",
    cache: false,
    headers: {          
        Accept : "application/json; charset=utf-8",         
        "Content-Type": "application/json; charset=utf-8"   
    },
    success: function (html){
        $( "#results" ).append( html ); //append response to an element with id 'results'

        // or

        $( "#results" ).html( html ); //replace contents of element with id 'results'
    },
    error: function (request, status, error) {
        alert(request.responseText);
        alert(status);
        alert(error);
    }
});

谢谢你,艾伦!你说得对!容易多了。我刚刚在一个方法中生成html时遇到了麻烦。我的直觉是,出于某种原因,它是原始的。我需要克服这一点。谢谢你,艾伦!你说得对!容易多了。我刚刚在一个方法中生成html时遇到了麻烦。我的直觉是,出于某种原因,它是原始的。我需要克服这一点。