-1

This is my server side output

[{"Id":"1","Username":"kamesh","Password":"kamesh","Email":"[email protected]","Employeeid":"hankamesh","Phonenumber":""}]

Now i am trying to access this Username and Password from my localhost using myhtml file. mylocal.html

$.ajax({
            url:"XXXX?callback=?",
            type:'GET',
            dataType:'json',                
            success:function(output){
            alert("working");
                    $.each(output,function(key,value){
                        $('#result').append('<p>Row '+key+' : UserName '+value.Username+'Password'+value.Password+'</p><br>');
                    });
            }
        });         
      });

I do not know whether it is correct or not. can anyone guide me please ..thanks in advance

2
  • @Ehswer..What is the error your are getting? Commented Feb 7, 2014 at 7:33
  • nothing is displaying... Commented Feb 7, 2014 at 7:37

3 Answers 3

1

Please add proper header in server side, sometimes it's create issue.. PHP

header("Content-type: text/json");
echo json_encode($array); 

And in jQuery, please make sure the response in json object... if response may be string not an object then please parse response in json object, There no need $.each loop, please try this please see blow

$.ajax({
            url:"XXXX?callback=?",
            type:'GET',
            dataType:'json',                
            success:function(output){
               var response_obj = {};
               try {
                 response_obj = jQuery.parseJSON(output)[0];
              }catch(e){alert('error==>' + e);}
                    $('#result').append('<p>Row username : UserName'+response_obj.Username+'Password'+response_obj.Password+'</p><br>');

            }
        });         
      });

I hope, issue would be resolved.

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

Comments

0
$.each(output,function(key,value){
                        $('#result').append('<p>'+key+': '+value+'</p><br>');
                    });

to access specific fields

$('#result').append('<p> UserName:'+output.Username+'Password :'+output.Password+'</p><br>');

3 Comments

here how can i split Username alone?
you are iterating in $.each() to fecth all the key value pairs in the row
if you want to access only Username you dont need the $.each()
0
   $.each(json, function() {
          $.each(this, function(name, value) {
            console.log(Name+ '=' + Value);

          });
        });

Can you just verify whether this code is giving you any results..

2 Comments

Datatype is set to JSON so no parsing required
i already set dataType to json... same kind of code i tried before it was working that time now it is not working ... i dono why

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.