Ajax.request using ExtJs

假装没事ソ 提交于 2019-12-07 11:10:45

问题


I'm having difficulty loading JSON content from a file. console.log prints "server-side failure with status code 0", meaning that the request failed.

My JSON file URL is correct and the file is present at the given address.

Firebug shows the error:

"NetworkError: 405 Not Allowed - http://localhost:8080/4910.json?_dc=1336714070619

and the Net tag shows status 405 not allowed.

Any ideas on why this is happening?

Ext.Ajax.request({
                   url: 'http://localhost:8080/4910.json',
                   success: function(response, opts) {
                      var obj = Ext.decode(response.responseText);
                      console.log(obj[0].name);
                   },
                   failure: function(response, opts) {
                      console.log('server-side failure with status code ' + response.status);
                   }
                });

Also, what is adding the parameter _dc=1336714070619 to the end of the GET request?


回答1:


Just FYI..

For the Who is adding parameter _dc. dc stands for disable cache.
by default, every request in extjs will automatically append the request variable with _dc. more info

If you don't want it, just set disableCaching: false to your request

Ext.Ajax.request({
    disableCaching: false,
    url: 'http://localhost:8080/4910.json',
    ......
});

For the why not allowed.
Try opening the JSON URL path without _dc, or maybe there is something wrong with your server.




回答2:


if the file you accessing is in the same domain, you can directly give the relative path there.

if the request is cross-domain: If you need to read data from another domain and can't set up a proxy server (some software that runs on your own domain's web server and transparently forwards requests to domainB.com, making it look like they actually came from domainA.com), you can use Ext.data.proxy.JsonP and a technique known as JSON-P (JSON with Padding), which can help you get around the problem so long as the server on domainB.com is set up to support JSON-P responses. See JsonPProxy's introduction docs for more details.

Check http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.proxy.JsonP




回答3:


I think you should use the relative path for the url (relative here is comparing to the js file), for example it should be 4910.json if the json file is at the same folder as your js file.




回答4:


Maybe, wrong server port?

This works good for me:

Ext.Ajax.request ({
    url: 'http://localhost:80/sof/json.json' ,
    disableCaching: false ,
    success: function (res) {
        // JSON is {name:val, type:val}
        console.log (Ext.JSON.decode(res.responseText).name);
    } ,
    failure: function () {
        console.log ('error');
    }
});



回答5:


Check about your webserver settings. My MAMP on OSX has localhost:8888.

Every time I use a file on localhost I use relative paths.

Have you tried to change the file permissions with CHMOD? Maybe you can't even open it in your browser?

Have you tried to write to the file from within php and checked afterwards?



来源:https://stackoverflow.com/questions/10545961/ajax-request-using-extjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!