Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2013
    Posts
    69
    Rep Power
    0

    Default How to create a Java Servlet for a given jQuery.ajax () call?

    I have a file called wfd.proxy.js that contains the following lines of code :
    Java Code:
        if (!WFD) { var WFD = {}; };
        if (!WFD.Proxy) { WFD.Proxy = {}; };
        
        WFD.Proxy = 
        {
            SERVICE_URL   : "/delegate/WFD/WFService?",
            PDF_SERVICE_URL : "/delegate/pdf-exporter?",
            DATA_TYPE     : "json", // used by jQuery
            DATA_TYPE_EXT : "ajax", // used by ExtJs
            DATA_TYPE_TXT : "text", // used for tests
        	SaveWorkflow : function(code)
        	{
        		jQuery.ajax({
        			url: WFD.Proxy.SERVICE_URL + "task=savemodel",
        			data: { code : code },
        			dataType : WFD.Proxy.DATA_TYPE,
        			type: 'POST',
        			success : function(data) {
        				WFD.Proxy.OnSaveWorkflowCallback(data);
        			},
        			error : function(jqXHR, textStatus, errorThrown) {
        				alert("Errore di comunicazione: " + errorThrown);
        			}
        		});
        	}
    
        ,
        	
        	WFD.Proxy.OnSaveWorkflowCallback = function(data) 
        	{
        		
        		/*
        			data.response
        			data.message
        			data.model_new_id
        			data.idsNodes[i].original_id
        			data.idsNodes[i].new_id
        		 */
        	}
        		
        };
    I have written the code that converts an xml file to JSON format. The JSON string that i get from the code I've written until now, should be passed as the code parameter of `SaveWorkflow : function(code)` .

    So I have this url:
    http:...delegate/WFD/WFService?task=savemodel&code=

    After code=
    I have to pass the JSON string, and this should return me "success" if all the content of JSON has been saved and an error instead.

    I'm not really sure what do I have to do at this point.
    I did some searches and saw that jQuery.ajax() calls where manipulated using Java Servlets ...

    Any idea how to resolve this please?
    Thanks in advance
    Last edited by blertadhimitri; 12-16-2013 at 03:50 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to create a Java Servlet for a given jQuery.ajax () call?

    Generally speaking, when you POST to a servlet, the data passed is not in the request header, it is in the request body(the data is not a part of the URL). So, your URL just needs to be the URL of your servlet, and all of the data will come in as parameters.

    On the server side, your servlet doPost() method will need to parse the json that you are passing as a parameter, make sense of it, and return the appropriate data:

    Here is an example of parsing JSON on the server side.

    Java Code:
    JSONObject jObj = new JSONObject(request.getParameter("data")); // this parses the json
    Iterator it = jObj.keys(); //gets keys
    
    while(it.hasNext())
    {
        String key = it.next(); // get key
        Object o = jObj.get(key); // get value
        //do something with the data
    }

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2013, 10:29 AM
  2. Jquery validation in struts with ajax
    By ashish1987 in forum Struts
    Replies: 6
    Last Post: 04-21-2012, 10:19 AM
  3. call a jquery function in <s:select > onChange method
    By sunithamm in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2011, 07:58 AM
  4. Twice Ajax Call on Firefox
    By javarabid in forum Web Frameworks
    Replies: 2
    Last Post: 08-28-2010, 07:02 PM
  5. How to Call servlet through Java client (from main)
    By akbjavauser in forum Java Servlet
    Replies: 3
    Last Post: 01-20-2010, 06:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •