Javascript ExtJS AJAX:XMLHttpRequest无法加载
ExtJS的新特性。 下面的所有文件都存在于同一个目录中(下面的代码)-不确定为什么会出现以下错误。即使数据存储在目录中,我也需要使用服务器吗? 我在Chrome控制台上获得以下信息:Javascript ExtJS AJAX:XMLHttpRequest无法加载,javascript,ajax,extjs,xmlhttprequest,Javascript,Ajax,Extjs,Xmlhttprequest,ExtJS的新特性。 下面的所有文件都存在于同一个目录中(下面的代码)-不确定为什么会出现以下错误。即使数据存储在目录中,我也需要使用服务器吗? 我在Chrome控制台上获得以下信息: XMLHttpRequest cannot load file:///C:/Users/Jeff/Development/workspace_extjs/Chapter%2007/JSON_data/data.json?_dc=1406036611330&page=1&start=0&li
XMLHttpRequest cannot load file:///C:/Users/Jeff/Development/workspace_extjs/Chapter%2007/JSON_data/data.json?_dc=1406036611330&page=1&start=0&limit=25.
Received an invalid response. Origin 'null' is therefore not allowed access. JSON_data.html:1
the server reported an error JSON_data.js:39
JSON\u data.js:
Ext.onReady(function(){
// Instantiate JSON store
var departmentStore = Ext.create('Ext.data.Store', {
fields: [
'name',
'active',
'dateActive',
'dateInactive',
'description',
'director',
'numEmployees',
{
name: 'id',
type: 'int'
}
],
// Configure AJAX proxy - communicates with data
proxy: {
type: 'ajax',
url: 'data.json',
// Configure JSON reader - reads and parses data
reader: {
type: 'json',
root: 'data',
idProperty: 'id',
successProperty: 'meta.success'
}
}
});
// Print out first record or error
departmentStore.load({
callback: function(records, operation, successful){
if (successful){
console.log('department name:', records[0].get('name'));
}
else {
console.log('the server reported an error');
}
}
});
});
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../lib/extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../../lib/extjs/ext-all-debug.js"></script>
</head>
<body>
<script type="text/javascript" src='JSON_data.js'></script>
</body>
</html>
{
"data" : [
{
"name" : "Media Relations",
"active" : "",
"dateActive" : "04/18/2013",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "7",
"id" : "1"
},
{
"name" : "Sales and Marketing",
"active" : "",
"dateActive" : "03/12/2012",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "9",
"id" : "2"
},
{
"name" : "Advertising",
"active" : "",
"dateActive" : "02/15/2013",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "6",
"id" : "100"
}
],
"meta" : {
"success" : true,
"msg" : ""
}
}
JSON\u data.html:
Ext.onReady(function(){
// Instantiate JSON store
var departmentStore = Ext.create('Ext.data.Store', {
fields: [
'name',
'active',
'dateActive',
'dateInactive',
'description',
'director',
'numEmployees',
{
name: 'id',
type: 'int'
}
],
// Configure AJAX proxy - communicates with data
proxy: {
type: 'ajax',
url: 'data.json',
// Configure JSON reader - reads and parses data
reader: {
type: 'json',
root: 'data',
idProperty: 'id',
successProperty: 'meta.success'
}
}
});
// Print out first record or error
departmentStore.load({
callback: function(records, operation, successful){
if (successful){
console.log('department name:', records[0].get('name'));
}
else {
console.log('the server reported an error');
}
}
});
});
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../lib/extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../../lib/extjs/ext-all-debug.js"></script>
</head>
<body>
<script type="text/javascript" src='JSON_data.js'></script>
</body>
</html>
{
"data" : [
{
"name" : "Media Relations",
"active" : "",
"dateActive" : "04/18/2013",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "7",
"id" : "1"
},
{
"name" : "Sales and Marketing",
"active" : "",
"dateActive" : "03/12/2012",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "9",
"id" : "2"
},
{
"name" : "Advertising",
"active" : "",
"dateActive" : "02/15/2013",
"dateInactive" : "",
"description" : "",
"director" : "",
"numEmployees" : "6",
"id" : "100"
}
],
"meta" : {
"success" : true,
"msg" : ""
}
}
Yeh看起来很像,谢谢-当我在Firefox中运行它时,我没有收到任何错误,但也没有得到成功的结果:“服务器报告了错误”嗯。好吧,也许你可以将
文件的URL绝对放在商店的URL
属性中?再次感谢,但结果相同:“服务器报告了错误”我需要在像Tomcat这样的东西里面运行这个吗?是的,很可能。如果您可以通过localhost/bla/bla/bla
访问页面,则url
属性的相对url也会要求localhost提供文件,该文件很可能会解决您的问题。