Ajax +跨域+渲染json文件

时间:2014-06-23 06:04:29

标签: jquery ajax json

I added ajax call in my local but i unable render response from api because i get cross domain issue. so i have one idea.I saved response in json file. so once i run the file means(in local) means we get response from in local json file if i run in server means i want to get response from the server. 
I dont want to manual checking for localhost/server.
Currently i used the code like this. but i want generic method for this.



 function checkLocalHost() {
    if (window.location.href.indexOf("localhost") > -1) {
        return true;
    } else {
        return false;
    }
}


if (checkLocalHost()) {
    var url = 'json/abcd.json';
} else {
    var url = 'http://abcd.com';
}


$.ajax({
    type : "POST",
    url : url,
    data : data
    contentType : "application/json; charset=utf-8",
    dataType : "json",
    success : function(data) {

    },
    error : function(error) {
        console.log('vvvv');
    }
});

在上面的代码中,我检查了域名是否为localhost。然后我运行localhost意味着我使用abcd json文件,否则我使用服务器api。

对此有何想法?

0 个答案:

没有答案