Solved

Jquery Ajax retun issue

Posted on 2013-06-14
3
269 Views
Last Modified: 2013-07-02
I'm not getting response as json type data from server.Please look into my code here...

    jQuery( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 500,
        width: 750,
        modal: true,
        buttons :{
            "Search" : function(){
                jQuery.ajax({type : 'POST',
                dataType : 'json',
                   url : '<s:url action="part" method="list" />',
               success : handledata})
            }
        }
    });
    var handledata = function(data)
    {
        alert(data);
    }

if dataType = 'json' am not getting any response, but if didn't mention anything dataType, getting html format of page.

     public String list(){
     JSONObject jo = new JSONObject();
      try {
             Iterator it = findList.iterator();
                 while(it.hasNext()){
                 SearchResult part = (SearchResult) it.next();
                 jo.put("col1",part.getcol1());
                 jo.put("col2",part.getcol2());
                 
                 }
                 log.debug--------->:"+jo.toString());
          
        } catch (Exception e) {
          log.error(e);
                              }
        return jo.toString();
        }

Output format:

    {"col1":"col1","col2":"col2"}

I'm not getting data to jQuery success.
Please correct me, whats wrong here ?
0
Comment
Question by:Rose_Taylor
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39250170
On your posted example you have:
url : '<s:url action="part" method="list" />',

I was expecting to see an actual url OR name of a server file here - ex:
url : 'myServerScript.php',

Also, I see:
...
  success:handledata
....

var handledata=function(data){...}

Open in new window


Notice that you are assigning the value of the variable handledata to success BEFORE you have even created the variable.  Thus, success is being assigned an undefined value.  

Try:
A.  Move up the definition so that handledata is defined BEFORE the line where you are making the ajax call:

var handledata=function(data){...}

jQuery( "#dialog-form" ).dialog({
...
});

Open in new window


OR
B.  Get rid of:
 var handledata = function(data)
    {
        alert(data);
    }

Open in new window

and instead change "success" to:
success : function (data)
              {
                alert(data);
              }

Open in new window


OR

C. change your definition of handledata so that it does not use the "var":
function handledata (data)
    {
        alert(data);
    }

Open in new window

0
 

Author Comment

by:Rose_Taylor
ID: 39250778
actually here search.jsp is the original file.but i have created popup window.so i have one more form in the same jsp file.
0
 

Author Closing Comment

by:Rose_Taylor
ID: 39295127
good
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question