Can I use ajax to call c#?

Why does the following AJAX call doesn't return new url from success and change URL in browser after returning from controller?

  • I am working on a Spring MVC project where the homepage has two input fields. Both input fields are of String type. But the regNo field gets number and if the user enters the regNo it should be taken to the corresponding method in controller. If the user enters the name, it should be taken to the corresponding method in controller. web.xml <web-app version="2.2" id="WebApp_ID"> <!-- <display-name>Archetype Created Web Application</display-name> --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/mvc/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/jsp/template.jsp</welcome-file> </welcome-file-list> </web-app> 1) What is the purpose of id="WebApp_ID"? tiles.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "Page on apache.org"> <tiles-definitions> <definition name="template" template="/WEB-INF/jsp/template.jsp"> <put-attribute name="title" value=""/> <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" /> <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp "/> <put-attribute name="center" value="/WEB-INF/jsp/ads.jsp" /> <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" /> </definition> <definition name="header" extends="template"> <put-attribute name="title" value="" /> <put-attribute name="body" value="/WEB-INF/jsp/ads.jsp" /> </definition> <definition name="numberResult" extends="template"> <put-attribute name="title" value="" /> <put-attribute name="body" value="/WEB-INF/jsp/numberResult.jsp" /> </definition> <definition name="nameResult" extends="template"> <put-attribute name="title" value="" /> <put-attribute name="body" value="/WEB-INF/jsp/nameResult.jsp" /> </definition> dispatcher-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="Index of /schema/beans" xmlns:xsi="Page on w3.org" xmlns:mvc="Index of /schema/mvc" xmlns:tx="Index of /schema/tx" xmlns:context="Index of /schema/context" xmlns:p="Page on springframework.org" xsi:schemaLocation="Index of /schema/mvc Page on springframework.org Index of /schema/tx Page on springframework.org Index of /schema/beans Page on springframework.org Index of /schema/context Page on springframework.org"> <context:component-scan base-package="com.ProjectCtxt.www.controller"/> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> --> <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/> <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass"> <value> org.springframework.web.servlet.view.tiles2.TilesView </value> </property> </bean> </beans> ProjectController.java @Controller("resultController") public class ResultController { private final ResultService resultService; @Autowired public ResultController(ResultService resultService) { this.resultService = resultService; } @RequestMapping(value ="/template", method = RequestMethod.GET) public String getPersonList(ModelMap model) { System.out.println("We are coming into this place"); return "header"; } @RequestMapping(value = "/search/s", method = RequestMethod.GET, params = { "regNo" }) public String getStudentResult(@RequestParam(value = "regNo", required = true) String regNo, ModelMap model){ System.out.println("I am coming here when I enter regNo and AJAX works"); model.addAttribute("studentResult",resultService.getStudentResult(regNo)); return "numberResult"; } @RequestMapping(value = "/search/l/studentName={studentName}", method = RequestMethod.GET, params = { "studentName" }) public String anotherMethod(String studentName, ModelMap model){ return "nameResult"; } } header.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <head> <script src="Page on jquery.com"></script> </head> <form method="GET" > <input type="text" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number"></input> <input type="text" id="studentName" name="studentName" size="30" maxLength="50" placeholder="Enter Student Name"></input> <button onclick="processInput();">Search </button> </form> <script> function processInput(){ if (document.getElementById('regNo').value !=""){ $.ajax({ type : 'GET', url : "search/s", data : { "regNo":$("#regNo").val()}, success : function(studentResult) { //show your result // alert("Value"); $('#displayArea').html(studentResult); } }); }else if (document.getElementById('studentName').value !=""){ $.ajax({ type : 'GET', url : "search/l", data : { "studentName":$("#studentName").val(), success : function(result) { //show your result }} }); } } </script> template.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="Page on apache.org" prefix="tiles"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <center> <table width="750" border="0" cellpadding="2" cellspacing="2" align="center"> <tr> <td><tiles:insertAttribute name="header" /> </td> </tr> <tr> <td><tiles:insertAttribute name="body" /></td> </tr> <tr> <td height="225"><tiles:insertAttribute name="footer" /> </td> </tr> </table> </center> </html> numberResult.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="Oracle Technology Network for Java Developers" %> <c:out value="This is working"/> <c:out value="${Page on studentresult.name}"/><br/> <c:out value="${studentResult.regNo}"/><br/> <c:out value="${studentResult.deptName}"/><br/> <c:out value="${studentResult.collName}"/><br/> <div id="displayArea"> <c:out value="${Page on studentresult.name}"/><br/> <c:out value="${studentResult.regNo}"/><br/> <c:out value="${studentResult.deptName}"/><br/> <c:out value="${studentResult.collName}"/><br/> </div> 2) User enters the regNo in header.jsp,and I want the result to be shown in numberResult.jsp 3) My first url is `localhost:8080/ProjectCtxt/mvc/template` goes to homepage. When I enter regNo in the input field, my url in browser shows `localhost:8080/ProjectCtxt/mvc/template/regNo=123&studentName=` . And I am reaching the getStudentResult method as I could see my System.out in console, but I am not able to see the output in browser. Since I am using tiles, I am not sure the problem is with tiles resolver or the AJAX. Could you please help me? Thanks. PS: Please answer all questions if possible with numbers so that it will help others. Thanks. UPDATE: I changed my header.jsp as follows and now I see "numberResult" in UI. But I don't see the numberResult jsp as body of template.jsp. I guess the "numberResult" text is returned to success part of AJAX. <input type="text" id="regNo" name="regNo" size="30" maxLength="50" placeholder="Enter Register Number"></input> <input type="text" id="studentName" name="studentName" size="30" maxLength="50" placeholder="Enter Student Name"></input> <input id="inputFields" type="button" value="Search" /> <script> $(document).ready(function(){ $('#inputFields').click(function(){ processInput(); }); }); function processInput(){ if (document.getElementById('regNo').value !=""){ $.ajax({ type : 'GET', url : "search/s", data : { "regNo":$("#regNo").val()}, success : function(studentResult) { //show your result alert("Value"); $('#displayArea').html(studentResult); //this line displays text //"numberResult". But it doesn't change the jsp as well as the url. Now my url looks as //localhost:8080/mvc/template and the body of the jsp doesn't have numberResult.jsp } }); }else if (document.getElementById('studentName').value !=""){ $.ajax({ type : 'GET', url : "search/l", data : { "studentName":$("#studentName").val(), success : function(result) { //show your result }} }); } } </script> <div id="displayArea"> <c:out value="${Page on studentresult.name}"/><br/> <c:out value="${studentResult.regNo}"/><br/> <c:out value="${studentResult.deptName}"/><br/> <c:out value="${studentResult.collName}"/><br/> </div>

  • Answer:

    I think your ajax call is not executing... check for it .

Deepak Pandey at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.