将JSON转换为字符串JAVASCRIPT
我希望我的位置显示为字符串。有人能告诉我怎么做吗?我刚接触过laravel和javascript。 我的ajax:将JSON转换为字符串JAVASCRIPT,javascript,json,Javascript,Json,我希望我的位置显示为字符串。有人能告诉我怎么做吗?我刚接触过laravel和javascript。 我的ajax: $(document).ready(function() { $('#country').on('change', function() { var country = this.value; if(country){ $.ajax({ url
$(document).ready(function() {
$('#country').on('change', function() {
var country = this.value;
if(country){
$.ajax({
url: '/member/page/press_release_email/choose_recipient_press_release/ajax',
type: "GET",
data:{country:country},
dataType: "json",
success:function(data) {
$('select[name="position"]').empty();
$.each(data, function(key, value) {
$('select[name="position"]').append('<option value="'+ JSON.stringify(key) +'">'+ JSON.stringify(value) +'</option>');
});
}
});
}else{
alert("as23d");
}
});
});
$(文档).ready(函数(){
$('#country')。on('change',function(){
var country=该值;
国际单项体育联合会(国家){
$.ajax({
url:“/member/page/press\u release\u email/choose\u recipient\u press\u release/ajax”,
键入:“获取”,
数据:{国家:国家},
数据类型:“json”,
成功:功能(数据){
$('select[name=“position”]).empty();
$。每个(数据、函数(键、值){
$('select[name=“position”]).append(''+JSON.stringify(value)+'');
});
}
});
}否则{
警报(“as23d”);
}
});
});
您正在字符串化值对象,但需要访问值对象的position属性:
$('select[name="position"]').append(
$('<option>', { value : value.position, text : value.position })
);
$('select[name=“position”]”)。追加(
$('',{value:value.position,text:value.position})
);
这还创建了一个option元素对象来设置文本和值,而不是连接可能导致XSS漏洞的HTML。如果在ajax响应中得到类似{key:value}的json,请检查以下内容:
...,
success: function(repsonse) {
$.each(repsonse, function(i, elem) {
$('select[name="position"]').append('<option value="'+ i +'">'+ elem +'</option>');
});
}
。。。,
成功:功能(repsonse){
$。每个(代表,功能(i,元素){
$('select[name=“position”]”)。追加(''+elem+'');
});
}
jsfiddle:
...,
success: function(repsonse) {
$.each(repsonse, function(i, elem) {
$('select[name="position"]').append('<option value="'+ i +'">'+ elem +'</option>');
});
}