使用jQuery读取JSON

I have a JSON result after Ajax as following

 {
"results": [

{
    "id": " 14903",
  "nsid" : "10438",
    "name": "001-2501220",
  "label":"001-2501220 End Seal",
  "price":"12.6"
}, 
{
    "id": " 14904",
  "nsid" : "10439",
    "name": "001-2501231",
  "label":"001-2501231 Poppet",
  "price":"12.3"
}, 
{
    "id": " 14917",
  "nsid" : "3391",
    "name": "PW16001",
  "label":"PW16001 Caliper Piston 38.1mm Lucas TRW handbrake",
  "price":"26.5"
} 
]}

I am facing difficulties reading this. I have tried as

$.ajax({
          type: "POST",
          url: url,
          data: "partnumber=" +value,
          cache: false,
          success: function(data){
            console.log(data);
            for (var i in data.results) {
                var name = data.results[i].name;
                var text = data.results[i].label;
                console.log(name);
                console.log(text);
            }
          },
          error: function(xhr, status, error) {
              var err = eval("(" + xhr.responseText + ")");
              alert(err.Message);
          }
        });

But no luck.

Make sure your JSON response is returned as data and not by any other name. Also since javascript is async you will need to perform this action in the jquery ajax success() function. You can also use it outside but that requires some you will need to handle that differently. One more thing add dataType: "json" in your ajax code, it will make sure your returned data is valid JSON and you may need to parse it

$.ajax({
          type: "POST",
          url: url,
          data: "partnumber=" +value,
          cache: false,
          dataType: "json",
          success: function(data){

            var data = JSON.parse(data);
            console.log(data);
            for (var i in data.results) {
                var name = data.results[i].name;
                var text = data.results[i].label;
                console.log(name);
                console.log(text);
            }
          },
          error: function(xhr, status, error) {
              var err = eval("(" + xhr.responseText + ")");
              alert(err.Message);
          }
        });

var data = {
"results": [

{
    "id": " 14903",
  "nsid" : "10438",
    "name": "001-2501220",
  "label":"001-2501220 End Seal",
  "price":"12.6"
}, 
{
    "id": " 14904",
  "nsid" : "10439",
    "name": "001-2501231",
  "label":"001-2501231 Poppet",
  "price":"12.3"
}, 
{
    "id": " 14917",
  "nsid" : "3391",
    "name": "PW16001",
  "label":"PW16001 Caliper Piston 38.1mm Lucas TRW handbrake",
  "price":"26.5"
} 
]}

$.each(data.results, function(key, val) {
console.log(val.name);
           console.log(val.id);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

</div>

Maybe your server doesn't send proper application/json Content-Type response header and jQuery doesn't know that it should automatically parse the response before passing it to the success callback. So if this is the case you could explicitly indicate the content type when making the request:

dataType: 'json'

Now assuming that this wrongly written server side script sends valid JSON, jQuery will properly parse it before feeding it to the success callback so that you can write the corresponding loop over the results elements.

This being said, I would recommend you fixing your server side script so that it correctly populates the Content-Type response. This would allow clients such as jQuery to properly handle the response.