简体   繁体   English

将 JSON 响应从 jquery ajax 提取到 html div

[英]Extract JSON response from jquery ajax to html div

I could not retrieve data to jquery div element but i can see ajax response in firebug.我无法将数据检索到 jquery div 元素,但我可以在 firebug 中看到 ajax 响应。

$(document).ready(function () {
    $.ajax({
        url: "https://m.lowes.com/CatalogServices/product/nvalue/v1_0?nValue=4294857975&maxResults=6&showURL=1&rollUpVariants=1&showUrl=true&storeNumber=0595&priceFlag=rangeBalance&showMarketingBullets=1&callback",
        dataType: "jsonp",
        success: function (data) {
            var returnedData = JSON.parse(data);
            $('#result').html(returnedData);
        }
    });
})

this is my response.for example my url contains data productCount:64 ,I should extract productCount from ajax success and display it in html div id result这是我的响应。例如我的 url 包含数据 productCount:64 ,我应该从 ajax 成功中提取 productCount 并将其显示在 html div id 结果中

When you parse out the JSOn data you use it like so:当您解析出 JSOn 数据时,您可以像这样使用它:

var parsed_data = JSON.parse(JSON_DATA);
$('#result').html(parsed_data.key);

So if your parsed_data is somewhat like:所以如果你的 parsed_data 有点像:

{name:"test",age:12}

then you use it like:然后你像这样使用它:

$('#result').html(parsed_data.name); //it will give you test

if you really want to print out the whole data use JSON.stringify(obj) like so:如果你真的想打印出整个数据使用JSON.stringify(obj)像这样:

$('#result').html(JSON.stringify(parsed_data));

In your code you are returning a json and you are trying to insert ino the div it, into the div you can only insert html code instead json.在您的代码中,您正在返回一个 json,并且您试图将其插入 div,在 div 中您只能插入 html 代码而不是 json。

Try to inspect the json you are returning and in case you need to insert each element into the div, do it, but don't do for all.尝试检查您返回的 json,如果您需要将每个元素插入 div,请执行此操作,但不要全部执行。

The html() method doesn't know what you want to assign, it only insert an html. html() 方法不知道您要分配什么,它只插入一个 html。

You need to iterate over your json data in returnData .您需要在returnData迭代您的 json 数据。 You are not able to put json directy into html.您无法将 json 直接放入 html。 You need to convert / parse / iterate it before.您需要先转换/解析/迭代它。 So, for example:因此,例如:

$.each(returnedData, function (index, value) {
    // do stuff with it, e. g.:
    //  $('#result').html($('#result').html() + value);
});

https://jsfiddle.net/m8udusps/ https://jsfiddle.net/m8udusps/

The code here actually works.这里的代码实际上有效。 However it says there is a problem when it is parsing the JSON.但是它说在解析 JSON 时出现问题。 You needed to add crossDomain: true, to allow it to actually get the results.您需要添加crossDomain: true,以允许它实际获得结果。

Having received a supposed JSON struct like:收到了一个假设的 JSON 结构,如:

{
  "Field1": "value1",
  "Field2": "value2"
}

This code gets the values of the keys (just first key in this example).此代码获取键的值(在本例中只是第一个键)。 Result is an alert message with Data: value1 :结果是一条带有Data: value1的警报消息:

                $.ajax({
                    'url' : './localurl',
                    'type' : 'GET',
                    'success' : function(data) {
                        alert("Data: "+data.Field1);
                    },
                    'error' : function(request,error) {
                        alert("Error in response: "+JSON.stringify(data));
                    }
                });

In this case Field1 is the key received and it is directly accesible through data variable, so it is not necessary to do the JSON.parse(data) (in fact this JSON.parse produce an error)在这种情况下Field1是接收到的密钥,它可以通过data变量直接访问,因此不需要执行JSON.parse(data) (实际上这个JSON.parse产生错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM