0

am calling a webservice from my jquery as

$.ajax({
        type: "POST",
        url: "/DataService.asmx/Search",
        contentType: "application/json; charset=utf-8",
        data: "{'date' : '" + $("#txtDate").val() + "'}",
        dataType: "json",
        success: function (data) {

        },
        error: function (req, status, error) {
        }
    });

in the data am receiving some values like "[{"Id":10,"Name":"abdul samathu","Mobile":"8147708287","Email":"shanish.mca@gmail.com"}]"

here how can I get the values alone, I tried like

var name=data.Name;
var mobile=data.Mobile like this, but its not working can anyone help me here
shanish
  • 1,964
  • 12
  • 35
  • 62
  • 1
    chck this...http://stackoverflow.com/questions/3445859/asmx-web-service-json-javascript-jquery/3446517#3446517 – Laxman Rana Apr 09 '12 at 09:31

4 Answers4

4

use

data[0].Name and data[0].Mobile

Riz
  • 9,703
  • 8
  • 38
  • 54
  • thanks for ur response, it says error like Name is null or undefined, I tried the code var name=data[0].Name; – shanish Apr 09 '12 at 09:46
  • you can check what is coming in data variable using console.log(data); Make sure data should be an object, not a string also should have Name parameter. Response format must be same otherwise we have to place conditions to get data accordingly. – Riz Apr 09 '12 at 10:10
  • if I use data=jQuery.parseJSON(data.d) its working fine, I can see the seperated arrays, but the problem is when I put the breakpoint here It shows the values at 0th index,within that 0th index again....then only the valus are displayed, how can I get the Name of Oth of 0th of data...... – shanish Apr 09 '12 at 12:09
2

What I am doing here is calling the web service, and looping the json objects and printing the items name. You can change it according to your needs, but this works.

$.ajax({
    url: "/DataService.asmx/Search",
    dataType: "json",
    type: "POST",
    cache: false,
    processData: false,
    data: "{'date' : '" + $("#txtDate").val() + "'}",
    contentType: "application/json; charset=utf-8",
    success: function (data) {

        if (data.d != null)
            data = data.d;

for (var i = 0; i < data.length ; i++) {
    console.log(data[i].Name);
}

    },
    error: function (x, y, z) { } // 
});
Mez
  • 4,666
  • 4
  • 29
  • 57
  • Instead of console.log(data[i].Name), put alert(data[i].Name). – Mez Apr 09 '12 at 09:48
  • if I use data=jQuery.parseJSON(data.d) its working fine, I can see the seperated arrays, but the problem is when I put the breakpoint here It shows the values at 0th index,within that 0th index again....then only the valus are displayed, how can I get the Name of Oth of 0th of data...... – shanish Apr 09 '12 at 12:09
  • 1
    If you use firebug, and view the console, you can drill down the objects and see what they contain. Console.log(data), so you can see the raw object. – Mez Apr 09 '12 at 12:19
1

Your data is an array of objects, not a single object. Use data[0].Name to access the first object's name.

Edit: based on your post, for some reason data looks like a string, not proper JSON-decoded array. Try the following:

data = jQuery.parseJSON(data);
console.log(data);
var name = data[0].Name;
console.log(name);
DCoder
  • 12,962
  • 4
  • 40
  • 62
  • hi DCoder thanks for ur response, I tried ur code,it says error like Name is null or undefined – shanish Apr 09 '12 at 09:48
  • after executing this line data = jQuery.parseJSON(data); the value of data becomes null, whats the problem – shanish Apr 09 '12 at 10:28
  • if I use data=jQuery.parseJSON(data.d) its working fine, I can see the seperated arrays, but the problem is when I put the breakpoint here It shows the values at 0th index,within that 0th index again....then only the valus are displayed, how can I get the Name of Oth of 0th of data...... – shanish Apr 09 '12 at 12:03
1

instead of $.ajax use $.getJSON() for usage please gone through following link http://api.jquery.com/jQuery.getJSON/

Nagaraju Badaeni
  • 890
  • 8
  • 14