1

Making request from javascript to Spring controller method as follows

<script language="javascript" type="text/javascript">
          var xmlHttp
          var xmlHttp
          function show()
          {   
              if(typeof XMLHttpRequest != "undefined")
              {
              xmlHttp= new XMLHttpRequest();
               }
              else if (window.ActiveXObject)
              {
                  xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
              }
              if(xmlHttp==null)
              {
                  alert("Browser does not support XMLHTTP Request")
                  return;
              }
              var FAC_LICENSE_NO=document.getElementById("FAC_LICENSE_NO").value;
              //var url="/Final/WEB-INF/jsp/SurrenderViews/Ajax.jsp";
              var url="http://localhost:8080/Final/Ajax.FSu";
              url +="?param1="+FAC_LICENSE_NO;
              alert(url);
              xmlHttp.onreadystatechange = stateChange;
              xmlHttp.open("GET", url, true);
              xmlHttp.send(null);         
            }   
          function stateChange()
          {   
                if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
                {
                    document.getElementById("factoryname").innerHTML=xmlHttp.responseText
                }
         }
    </script>

and my controller

public class PhaseTwoFormSurrenderOfLicense extends MultiActionController implements Connections {

     public ModelAndView DataInput(HttpServletRequest request,HttpServletResponse response) 
     {
         return new ModelAndView("SurrenderViews/DataInput");
     } 
     public String Ajax(HttpServletRequest request,HttpServletResponse response) 
     {
           System.out.println("Maritammanafvara");
           String returning="<input type=\"text\" style=\"border: none\" name=\"Factory_name\" readonly=\"readonly\" value=\"HIHI\">";
           return returning;

     } 
}

and in the above code I am able to call both DataInput and Ajax methods from html anchor tag, but calling from XMLHttpRequest(ajax) object not going. Can any one tell the region?

3 Answers 3

1

can you try var url="Ajax.FSu";

instead of

var url="http://localhost:8080/Final/Ajax.FSu";

Sign up to request clarification or add additional context in comments.

1 Comment

can you use a browser tool like firebug and see if a request is going
0

Have you thought of using a library to use for AJAX? jQuery has handy methods for this: api.jquery.com/category/ajax/

Example:

$.ajax("http://localhost:8080/Final/Ajax.FSu").done(function (data) {
    // do stuff with data
}).fail(function () {
    // do stuff when failed
});

Also you'd need to encode a proper URL, not hardcode the localhost part there. Spring MVC has a handy tag for this:

<spring:url value="Final/Ajax.FSu" />

will output the URL for you and

<spring:url value="Final/Ajax.FSu" var="yourURL" />

will insert the URL in the context variable to use in your jsp files like this:

<a href="${yourURL}">Link</a>

2 Comments

Thats answer not related my question sir.
I just saw this code of handrafted XmlHttpRequest code and immediately thought that you might want to use a library to handle all the quirks for you - I thought this might solve your problem in the process. If this is an unwanted suggestion though, then I stand corrected - sorry.
0

I dont see your methodNameResolver, which Spring uses to route URLs to MultiActionController's, it might be something like PropertiesMethodNameResolver or one created by yourself. This is my dispatcher:

    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
    <property name="paramName" value="method" />
</bean>


<bean class="your.controller">
    <property name="methodNameResolver" ref="methodNameResolver" />
</bean>


public class YourController extends MultiActionController

    public ModelAndView abcdef(...){

    }
}

this will map to url?method=abcdefg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.