简体   繁体   English

GET请求从Ajax弹出

[英]GET request to spring from ajax

I am learning ajax and javascript. 我正在学习ajax和javascript。 This is addition of 2 numbers using spring and ajax. 这是使用spring和ajax加2的数字。 I am getting The request sent by the client was syntactically incorrect error. 我正在获取客户端发送的请求在语法上是错误的错误。 Please help me. 请帮我。

Controller class 控制器类

@Controller
public class SumWithAjaxSpringController {

    @Resource(name="sumWithAjaxService")
    private SumWithAjaxService sumWithAjaxService;

 @RequestMapping(value = "additionWithAjax", method = RequestMethod.GET)
     public String add(@RequestParam(value="value1", required=true) Integer value1,
                        @RequestParam(value="value2", required=true) Integer value2,) {

         Integer sum =  springService.add(value1, value2);

            return "additionWithAjax";
        }
}

service class 服务等级

@Service("sumWithAjaxService")
@Transactional
public class SumWithAjaxService {

    public Integer add(Integer number1, Integer number2) {

        return number1+ number2;
    }
}

JSP JSP

    <script type="text/javascript">
    function add() 
    {
        var xmlhttp;
        var value1 = document.getElementById("text1").value;
        var value2 = document.getElementById("text2").value;
        var url = "additionWithAjax";
        var parameters = "text1=" + value1 + "&text2=" + value2;

        if (window.XMLHttpRequest)
          { 
          xmlhttp=new XMLHttpRequest();
          }
        else
          { 
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }

        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("result").innerHTML=xmlhttp.responseText;

            }

          };
            xmlhttp.open("GET",url+"?"+parameters,true);
            xmlhttp.send();
        }
        </script>        
    </head>
    <body>

Enter 1st number : <input type="text" name="n1" id="text1"> 
Enter 2nd number : <input type="text" name="n2" id="text2"> 
<input type="button" id="calculate" value="calculate"
                    onclick="add()" />
Result :<span id="result" > </span>

</body>

web.xml web.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>

@ResponseBody is missing . @ResponseBody丢失。

The below method need to be re written 下面的方法需要重写

 @RequestMapping(value = "additionWithAjax", method = RequestMethod.GET)
     public String add(@RequestParam(value="value1", required=true) Integer value1,
                        @RequestParam(value="value2", required=true) Integer value2,) {

         Integer sum =  springService.add(value1, value2);

            return "additionWithAjax";
        }

into 进入

@RequestMapping(value = "additionWithAjax", method = RequestMethod.GET)
     public @ResponseBody String  add(@RequestParam(value="value1", required=true) Integer value1,
                        @RequestParam(value="value2", required=true) Integer value2,) {

         Integer sum =  springService.add(value1, value2);

            return Integer.toString(sum );
        }

Change your controller method to following and try again. 将您的控制器方法更改为以下方法,然后重试。

@RequestMapping(value = "additionWithAjax/{value1}/{value2}", method = RequestMethod.GET)
public String add(@PathVariable("value1") Integer value1,
                  @PathVariable("value2") Integer value2) {
}

Send these values from your ajax URL. 从您的Ajax URL发送这些值。 And while getting service instance in controller, you could use @autowired annotation instead of @Resource . 在控制器中获取服务实例时,可以使用@autowired注释代替@Resource

@Autowired
@Qualifier("sumWithAjaxService")
private SumWithAjaxService sumWithAjaxService;

From what I found in your request URL, You need to first handle the '/SumWithAjaxController' request mapping for your controller like this, 根据我在请求网址中找到的内容,您需要先像这样处理控制器的“ / SumWithAjaxController”请求映射,

@Controller
@RequestMapping(value = "/SumWithAjaxController")
public class SumWithAjaxSpringController {

and then you have to add the remaining part '/additionWithAjax' to your method, 然后您必须将其余部分'/ additionWithAjax'添加到您的方法中,

@RequestMapping(value = "/additionWithAjax", method = RequestMethod.GET)

Also there is one mistake in your js code that, you are adding the two parameters with names 'text1' and ''text2'. 在您的js代码中,还有一个错误是,您要添加两个名为'text1'和'text2'的参数。 They will not be assigned to your controller method's parameter names with 'value1' and 'value2'. 它们不会通过'value1'和'value2'分配给控制器方法的参数名称。 The parameter names must match. 参数名称必须匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM