[英]fetch json object containing 3 arrays with ajax call and pass arrays to javascript
I have a page that creates the following output: 我有一个页面,可以创建以下输出:
<script>
var JSONObject = { "groups":['1210103','1210103','1210103','1210405'],
"prices":['279,00','399,00','628,00','129,00'],
"titles":['','','','']
};
</script>
This page is called by an ajax call: 该页面由ajax调用调用:
$.ajax({url:plink,success: function(result) { }
I now need to recieve the json arrays and pass them to ordinary javascript arrays. 我现在需要接收json数组并将它们传递给普通的javascript数组。 How do I do that? 我怎么做?
I have tried with: 我尝试过:
result = jQuery.parseJSON(data);
mygroups = result.groups;
myprices = result.prices;
mylabels = result.titles;
Change your page so that it just produces JSON: 更改页面,以便它只是产生JSON:
{"groups":["1210103","1210103","1210103","1210405"],
"prices":["279,00","399,00","628,00","129,00"],
"titles":["","","",""]
}
Note that in JSON, you must use "
, not '
, for quoting strings. 请注意,在JSON中,您必须使用"
,而不是'
来引用字符串。
Have it return a Content-Type
header of application/json
. 让它返回application/json
的Content-Type
标头。 If for some reason you can't set the correct Content-Type
header on the response, you can force jQuery to treat the response as JSON by adding dataType: 'json'
to your ajax
call, but it's best to use the correct content-Type
. 如果由于某种原因你无法在响应上设置正确的Content-Type
标头,你可以强制jQuery通过在你的ajax
调用中添加dataType: 'json'
来将响应视为JSON,但最好使用正确的content-Type
。
Then in your ajax
call's success
callback, result
will already be a deserialized object with three properties ( groups
, prices
, titles
), which will be JavaScript arrays you can work with. 然后在你的ajax
调用的success
回调中, result
将已经是一个反序列化的对象,它有三个属性( groups
, prices
, titles
),这将是你可以使用的JavaScript数组。
Live Example | 实例 | Source 资源
You've said in the comments below that the page is a full HTML page with the embedded script
tag and you have no control over it other than the contents of the script
tag, because of the CMS you're using. 你在下面的页面与嵌入式完整的HTML页面的评论说, script
标签,你必须在它比的内容之外,没有其他控制script
标签,因为CMS的你使用。
I strongly suggest moving to a more flexible CMS. 我强烈建议转向更灵活的CMS。
Until/unless you can do that, you can simply receive the page as text and then extract the JSON. 除非您可以这样做,否则您只需将页面作为文本接收,然后提取JSON即可。 Change your script
tag to something like this: 将script
标记更改为以下内容:
<script>
var JSONObject = /*XXX_JSONSTART_XXX*/{"groups":["1210103","1210103","1210103","1210405"],
"prices":["279,00","399,00","628,00","129,00"],
"titles":["","","",""]
}/*XXX_JSONEND_XXX*/;
</script>
Note the markers. 注意标记。 Then you can extract the JSON between the markers, and use $.parseJSON
on it. 然后,您可以在标记之间提取JSON,并在其上使用$.parseJSON
。 Example: 例:
(function($) {
$.ajax({
url: "http://jsbin.com/ecolok/1",
dataType: "text",
success: function(result) {
var startMarker = "/*XXX_JSONSTART_XXX*/";
var endMarker = "/*XXX_JSONEND_XXX*/";
var start, end;
start = result.indexOf(startMarker);
if (start === -1) {
display("Start marker missing");
}
else {
start += startMarker.length;
end = result.indexOf(endMarker, start);
if (end === -1) {
display("End marker missing");
}
else {
result = $.parseJSON(result.substring(start, end));
display("result.groups.length = " + result.groups.length);
display("result.prices.length = " + result.prices.length);
display("result.titles.length = " + result.titles.length);
}
}
}
});
function display(msg) {
$("<p>").html(String(msg)).appendTo(document.body);
}
})(jQuery);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.