March 2, 2012

how to read the object value in jquery

Question by user1195292

I want to read the object value in jquery but if am trying to use text function it shows me blank.The code is as below :

var param = $(this).find('parameter');

                  if(param.attr('pathvar')){
                    var pathvar=param.attr('pathvar');
                    url=url+'/'+pathvar
                    alert(url);
                  }
                  var numLow = 0;
                  var numHigh = (param.length-1);                  
                  var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
                  var numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);   
                      alert(numRand); 
                      alert("here");  
                      alert(param);   
                  var selParamVal = param.eq((numRand)).text();
                   alert(param.eq((numRand)).text());
makeAPIRequest(selParamVal,requestType,url);                                 
                }
          });

        } 
    }  

function makeAPIRequest(reqjson,reqType,url){
    var json = JSON.stringify(reqjson); 

    $.ajax({
         url:url,    
         type:reqType,
         processData: false,  
         contentType:"application/json; charset=utf-8",
             data:reqjson,
         headers:{sm_user:'999999300'},
         success:function(data){
           var resp = JSON.stringify(data);
           var textarea = jQuery('#responseEl');
           textarea.text(resp);  
        },
       error:function(jqXHR, textStatus, errorThrown){
            var textarea = jQuery('#responseEl');
            textarea.text(jqXHR.responseText);
     }
    });

  }

The selParamVal is blank.And if even if i add numRand as [numRand] it does not make an ajax call
Trying to read this xml :I am able to get upto url but fails in parameter

<?xml version="1.0" encoding="UTF-8"?>
<apis xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <api name="getFieldType" requesttype ="GET">
         <url>/fieldtype/</url>       
         <parameters>
             <parameter><![CDATA[fieldtype=textbox&fieldId=8668376496495]]></parameter>
             <parameter><![CDATA[fieldtype=number&fieldId=93468613046610]]></parameter>
             <parameter><![CDATA[fieldtype=multiline&fieldId=1367537832000]]></parameter>
          </parameters>
    </api>
</apis>

Answer by Starx

I think your problem is this

$(this).find('parameter');

It is neither an identifier nor a class selector, or an element name

Changes it to appropriate selector. Like the attribute below will select class parameter

$(this).find('.parameter');

Update: I got your code working. The problem seems to be when $(this) was not referencing to the xml data.

Check Here

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!