0

This is my data:

[{"ServiceId":4,"ServiceName":"first sevice","ServiceCriteria":"Lumpsum","ItemBasis":"Inward/Outward","Currency":null,"Amount":null}]

How can I read this data when it's passed back from a controller in the success?

  $('#btnservice').click(function () {
        var url = "/Quotation/SelectService/";
        var selservice = $("#selservice").val();
        $.ajax({
            url: url,
            data: { selservice: selservice },
            cache: false,               
            dataType: 'json',
            type: "POST",
            success: function (data) {               

                $("#Services").empty();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    });

4 Answers 4

1

you can read like this

 var a = [{ "ServiceId": 4, "ServiceName": "first sevice", "ServiceCriteria": "Lumpsum", "ItemBasis": "Inward/Outward", "Currency": null, "Amount": null}];
        alert(a[0].ServiceId);
Sign up to request clarification or add additional context in comments.

Comments

0

Your controller side must be like this

public string SelectService(string ServiceId, string ServiceName, string ServiceCriteria, string ItemBasis, string Currency, string Amount)
{
    string message = string.Empty;
    //do whatever you want here and return message like 
    message=success;
    return message;
}

Than in view page check if return value is success or not.

    success: function (data) {               
         if(data=="success" )
         {
            //to do
            $("#Services").empty();
         }
         else
         { 
            //bla bla
         }
   },
   error: function (reponse) {
   alert("error : " + reponse);
   }

Comments

0

I'm assuming that your JSON string is returned by the controller. You can access the return data in the success callback. Your data variable already contains this response.

$('#btnservice').click(function () {
    var url = "/Quotation/SelectService/";
    var selservice = $("#selservice").val();

    $.ajax({
        url: url,
        data: { selservice: selservice },
        cache: false,               
        dataType: 'json',
        type: "POST",
        success: function (data) {               
            var service = data[0];

            alert(service.ServiceName); // Should be "first service"

            $("#Services").empty();
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });
});

Comments

0

I use following code and it works good:

success: function (data) {
     $.each(data, function (index, item) {
     alert(data[index].ServiceId)
     alert(data[index].ServiceName)    
     .............................
     .............................
     //other properties you want to get                      
     });
    }

Comments

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.