Vasu
Vasu

Reputation: 365

how can I parse ajax response in jquery?

I want to know how can we parse the response from the ajax in jquery script.

EDITED :

Here is my jquery : jquery_test.jsp

 <h1>Hello World!</h1>

    <select id="body_id" name="current_session" >
            <option value="Winter">Winter</option>
            <option value="Monsoon">Monsoon</option>
    </select>

     <script>
      $( "#stream" ).change(function() {
           var selectedVal=$("#stream option:selected").val();
            $.ajax({
                 url:"checkonserver.jsp?current_session="+selectedVal,
                 success:function(msg){
                 alert(msg);
                   }
                });
              });
    </script>
    </body>

here is my ajax code (checkonserver.jsp)

    <body id="body_id" >
    <% if(request.getParameter("current_session").toString().equals("Winter")){%>
        It's COLD
     <% 
     }
       else{
      %>
      It's HOT
    <%}%>
    </body>

i got msg from ajax to jquery as fallow ( .jsp page) // it you select monsoon in list

<head>
    <body>
        It's HOT         
    <\body>
<\head>

my question is how can i parse the msg to get data inside the body of above jsp. for example : only its HOT i want as output on the browser. not entire html file as above

Upvotes: 2

Views: 8351

Answers (3)

Rajiv Ranjan
Rajiv Ranjan

Reputation: 1869

Under body tag, all data you can get using Jquery:

var updatedData = msg;
jQuery(updatedData).find('body').html();

Update jquery_test.jsp page:

Remove:

url:"checkonserver.jsp?current_session="+selectedVal,
success:function(msg){
    alert(msg);
}

Add:

url:"checkonserver.jsp?current_session="+selectedVal,
dataType : 'html',
success:function(msg){
    var updatedData = msg;
    alert(jQuery(updatedData).find('body').html());
}

Upvotes: 2

defau1t
defau1t

Reputation: 10619

1. To get JSON From An Array of objects use this

     $.ajax({
                      type:"GET",
                      dataType:"json",
                      url:"thejson",
                      success: function(data) {
                           $.each(data, function(index,element){
                              alert(element.Device);
                         });
                       },
                      error: function() {
                        alert("Not Found File");
                      }
                    });



    JSON Could be like
    [
        {
            "Device": "xklklklx",
            "Count": 5
        }
    ]

2. To get JSON from Objects Use this:

     $.ajax({
                  type:"GET",
                  dataType:"json",
                  url:"thejson",
                  //data:
                  success: function(data) {
                    alert(data.Device);
                  },
                  error: function() {
                    alert("Not Found File");
                  }
                });

    JSON Can be like:
     {
            "Device": "Some Device",
            "Count": 5
        }

Upvotes: 1

step 1 : give id to field

<html>
    <body id="body_id" name="body_name">
        <h1>field comes here</h1>
    <\body>
<\html>

step 2 : you can follow any of the code below

$.ajax({
    url: "checkonserver.jsp?current_session=" + selectedVal,
    success: function(msg){
        //alert(msg)
        document.getElementById('body_id').innerHTML = msg; // or
        document.getElementByName('body_name').innerHTML = msg; //or
        JQuery('#body_id').value = msg; //or
        JQuery('#body_id').innerHTML = msg; 
    }
});

Upvotes: 0

Related Questions