Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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/5/actionscript-3/6.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
Jquery 获取JSon请求时出错;406后(不可接受)_Jquery_Json_Spring Mvc - Fatal编程技术网

Jquery 获取JSon请求时出错;406后(不可接受)

Jquery 获取JSon请求时出错;406后(不可接受),jquery,json,spring-mvc,Jquery,Json,Spring Mvc,尽管我已经搜索了这个网站并尝试了所有的方法,但还是不断地犯上面的错误。非常感谢你的帮助。问题在于AddUser页面上的Jquery/JSON请求。我已经添加了两个jacksonjar文件(核心和映射),它部署在tomcat7上 这是配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/

尽管我已经搜索了这个网站并尝试了所有的方法,但还是不断地犯上面的错误。非常感谢你的帮助。问题在于AddUser页面上的Jquery/JSON请求。我已经添加了两个jacksonjar文件(核心和映射),它部署在tomcat7上

这是配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
        <property name="cacheSeconds" value="3000" />
    </bean>   

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="ajaxjqjsonbowling" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
以下是chrome调试器的标题:

Request URL:http://localhost:8080/ajaxjqjsbowling/AddUser.htm
Request Method:POST
Status Code:406 Not Acceptable
Request Headersview parsed
POST /ajaxjqjsbowling/AddUser.htm HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 15
Accept: */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:8080/ajaxjqjsbowling/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=E7FBB01B3FA25CFC091AA539EDFE3CC9
Form Dataview sourceview URL encoded
name:John Horan


Response Headersview parsed
    HTTP/1.1 406 Not Acceptable
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=utf-8
    Content-Length: 1067
    Date: Mon, 06 May 2013 00:24:53 GMT
这是我的JQuery代码:

function doAjaxPost() {  
      // get the form values  
      var name = $('#name').val();

      $.ajax({  
        type: "POST",  
        url: contexPath + "/AddUser.htm",  
        data: "name=" + name,  
        success: function(response){
          // we have the response 
          if(response.status == "SUCCESS"){
              userInfo = "<ol>";
              for(var i =0; i < response.result.length ; i++){
                  userInfo += "<br><li><b>Name</b> : " + response.result[i].name;
              }
              userInfo += "</ol>";
              $('#info').html("User has been added to the list successfully. " + userInfo);
              $('#name').val('');         
              $('#error').hide('slow');
              $('#info').show('slow');
          }else{
              errorInfo = "";
              for(i =0 ; i < response.result.length ; i++){
                  errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
              }
              $('#error').html("Please correct following errors: " + errorInfo);
              $('#info').hide('slow');
              $('#error').show('slow');
          }       
        },  
        error: function(e){ 
              alert('Error '+ e);
        }  
      });  
    }
函数doAjaxPost(){
//获取表单值
var name=$('#name').val();
$.ajax({
类型:“POST”,
url:contexPath+“/AddUser.htm”,
数据:“name=“+name,
成功:功能(响应){
//我们得到了答复
如果(response.status==“SUCCESS”){
userInfo=“”;
对于(var i=0;i
  • 名称:”+response.result[i].Name; } 用户信息+=“”; $('#info').html(“用户已成功添加到列表中。”+userInfo); $('#name').val(''); $('#error')。隐藏('slow'); $('info')。显示('slow'); }否则{ errorInfo=“”; 对于(i=0;i”+(i+1)+“+响应。结果[i]。代码; } $('#error').html(“请更正以下错误:“+errorInfo”); $('info')。隐藏('slow'); $('#error')。show('slow'); } }, 错误:函数(e){ 警报(“错误”+e); } }); }
  • AddUser.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <style>
    
    h1 {
        font-style: italic;
        color: green;
        font-family: Arial, Helvetica, sans-serif;
        }
    
    * {
        color: blue;
        font-family: Arial, Helvetica, sans-serif;
    }
    
    </style>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Ajax Jquery Bowling Game</title>
    
    <script src="<%=request.getContextPath() %>/js/jquery.js"></script>
    
    <script type="text/javascript">
        var contexPath = "<%=request.getContextPath() %>";
    </script>
    
    <script src="<%=request.getContextPath() %>/js/player.js"></script>
    
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/style/app.css">
    </head>
    
    <body>
    
    <div id = "topbox" style="width: 900x; height: 75px; background-color : #B7F39F">
    <span id = "title"> Ajax-JQuery Bowling</span>
    </div>
    
    <h1>Welcome to The Bowling App!</h1>
        <table>
            <tr><td colspan="2"><div id="error" class="error"></div></td></tr>
            <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr>
            <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
            <tr><td colspan="2"><div id="info" class="success"></div></td></tr>
        </table>
    <form:form method="post" action="/ajaxjqjsbowling/showPlay.htm">
        <tr>
            <td colspan="2">
                <input type="submit" name = "button" value="Play"/>
            </td>
        </tr>
    </form:form>
    </body>
    </html>
    
    
    h1{
    字体:斜体;
    颜色:绿色;
    字体系列:Arial、Helvetica、无衬线字体;
    }
    * {
    颜色:蓝色;
    字体系列:Arial、Helvetica、无衬线字体;
    }
    ajaxjquery保龄球游戏
    var=”;
    ajaxjquery保龄球
    欢迎使用保龄球应用程序!
    输入您的姓名:


    我编写了返回json的代码,但采用了如下字符串数据类型格式:

    [
        {
            "name": "Jason",
            "number": "10"
        },
        {
            "name": "Jimmy",
            "number": "11"
        }
    ]
    
    寻找

    其功能大致如下:

    @RequestMapping(value = "product", method = RequestMethod.GET, headers = "Accept=*/*")
    public @ResponseBody
    String addUser(@RequestParam("term") String query) {
        String json = "[{\"name\": \"Jason\",\"number\": \"10\" }, {\"name\": \"Jimmy\",\"number\": \"11\" }]";
        System.out.println(json);
        return json;
    }
    

    我不使用杰克逊或罐子。对不起,我只知道我的方式,可能其他人可以解释得更清楚。

    您有正确的请求内容类型标题吗?你能发布你的jQuery代码吗?刚刚添加了这个,还有AddUser.jsp页面。在ajax调用中设置
    dataType:'json'
    ,可能会有帮助,因为406状态代码引用了不可接受的
    Accept
    头。@shakib尝试过了,仍然不起作用。
    @RequestMapping(value = "product", method = RequestMethod.GET, headers = "Accept=*/*")
    public @ResponseBody
    String addUser(@RequestParam("term") String query) {
        String json = "[{\"name\": \"Jason\",\"number\": \"10\" }, {\"name\": \"Jimmy\",\"number\": \"11\" }]";
        System.out.println(json);
        return json;
    }