我以前版本的这个 ajax 代码在测试过程中完美地工作,google-chrome --disable-web-security
用选项填充选择元素但是现在我重写了代码以使用 jsonp 它不会工作,我不知道如何修复它。
jQuery.ajax({
url: xmlrpcproxy + '?callback=jsonpcallbackfunc', // <--- returns valid json if accessed in the browser
type: "GET",
cache: false,
contentType: "jsonp", // Pay attention to the dataType/contentType
dataType: 'jsonp', // Pay attention to the dataType/contentType
jsonpCallback: 'jsonpcallbackfunc',
});
function jsonpcallbackfunc (data) {
var firstoption = '<option value="select" selected>Please Select</option>';
jQuery("select#ItemIDSelect").html(firstoption);
var i;
var erplist = JSON.parse(data);
alert(data);
for (i = 0; i < erplist.length; ++i) {
jQuery("select#ItemIDSelect").append('<option value="' + erplist[i] + '">' + erplist[i] + '</option>');
}
}
被调用服务器页面返回
jsonpcallbackfunc(["admin2", "Administrator", "Afdeling XCV", "Agrolait", "Angel Cook", "Arthur Gomez", "ASUSTeK", "Axelor", "Axelor", "Ayaan Agarwal", "Bank Wealthy and sons", "Benjamin Flores", "Best Designers", "Brian Williams", "Camptocamp", "Chamber Works", "Chao Wang", "Charlie Bernard", "China Export", "Clement Marsot", "Coin gourmand", "Daniel Jackson", "David Simpson", "Delisle Albert", "Delta PC", "Demo User", "Donald Thompson", "Edward Foster", "Elec Import", "Epic Technologies", "Fabian Semal", "George Wilson", "Global Solutions", "Jacob Taylor", "Jacque Marseille", "James Miller", "Jan Bart", "Jessica Dupont", "John M. Brown", "Joseph Walters", "Julia Rivero", "Kevin Clarke", "Laith Jubair", "Laura Castro", "Lucas Jones", "Luc Maurer", "Luminous Technologies", "Mark Davis", "Marketing Business", "Mediapole", "Michel Fletcher", "Millennium Industries", "Morgan Rose", "Natuurpunt vzw", "Nebula Business", "Nhomar Hernandez", "Paul Williams", "Peter Mitchell", "Phillipp Miller", "Pizza Inn", "Richard Ellis", "Robert Anderson", "Robin Smith", "Roger Scott", "Seagate", "Sergio P\u00e9rez", "Spark Systems", "Steven Hamilton", "Tang Tsui", "Template User", "The Jackson Group", "The Poste", "Think Big Systems", "Thomas Passot", "Tom Pintens", "Vauxoo", "Vicking Direct", "William Thomas", "Your Company", "Your Company, Birmingham shop", "Your Company, Chicago shop", "Zhi Ch'ang"])
更新:该函数现在触发并将静态选项添加到选择元素。但是var erplist = JSON.parse(data);
得到了意外的令牌 a(来自 admin2)我需要更改什么才能使 JSON.parse(data) 不失败,我是否以某种方式更改数据?
感谢您的阅读!