无法从服务器读取JSON响应 [英] Can't read JSON response from server

查看:132
本文介绍了无法从服务器读取JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些将数据发送到网站的网络应用程序。该网站将数据更新到其数据库并返回一个替换我的webapp页面的json数组。我正在使用ajax进行查询。我需要知道如何防止覆盖我的网页。服务器不是我的,所以可能有相同的原产地策略问题。

我检查了 xmlhttp.readystate xmlhttp .status ,它们分别是4和0。根据stackoverflow上的一些帖子,由于起源策略,0状态发生,但我无法得到解决方案,因为通常有这个问题的人有权访问服务器端编程的变化。

I想要读取json并为我的应用程序提取值,但如果使用 xmlhttp.responseText ,服务器将返回一个空字符串。



$ p
谢谢



我的代码:

  function sendAndReceive(){

var xmlhttp;
xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function(){
document.getElementById(postevent)。innerHTML =state changed;
document.getElementById(postevent)。innerHTML =+ xmlhttp.readyState ++ xmlhttp.status;
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
document.getElementById(postevent)。innerHTML =success response;
document.getElementById(postevent)。innerHTML =this is it+ xmlhttp.responseText;
}
}

var url =http://simple.ap.ernet.in/api/home/resource.php/resource/node?;
url + =key =+ document.getElementById(key)。value +&;
url + =datasetId =+ document.getElementById(datasetId)。value +&;
url + =localId =+ document.getElementById(localId)。value +&;
url + =nodeName =+ document.getElementById(nodeName)。value +&;
url + =nodeDesc =+ document.getElementById(nodeDesc)。value +&;
url + =lat =+ document.getElementById(lat)。value +&;
url + =long =+ document.getElementById(long)。value;

document.getElementById(postevent)。innerHTML = url;
xmlhttp.open(POST,url,true);
xmlhttp.send();


解决方案

唯一已知的解决方法是让浏览器认为只有一个来源。最简单的方法是在您自己的服务器上放置一个代理服务器,然后中继到另一台服务器。如果您的服务器在Apache中,则可以使用 mod_proxy (请参阅此问题)。



但请注意,如果其他服务器不允许交叉源请求,则可能违反其政策。如果它是一个大型服务器,它可能会很好地检测到它并禁止你的服务器的IP。


I am working on a web app that sends some data to a website. The website updates the data to its database and returns a json array that replaces my webapp page. I am using ajax for making the query. I need to know how to prevent the overwriting of my webpage. The server is not mine so there is possibly a same origin policy problem.
I have checked the xmlhttp.readystate and xmlhttp.status, they are 4 and 0 respectively. According to some posts on stackoverflow the 0 status occurs due to the origin policy but I couldn't get a solution for this because usually the people with this problem had access to changes on the server side programming.
I want to read the json and extract values for my app but if I use the xmlhttp.responseText the server returns a blank string.

Any help is much appreciated.
Thanks

My code:

function sendAndReceive() {

    var xmlhttp;
    xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function () {
        document.getElementById("postevent").innerHTML = "state changed";
        document.getElementById("postevent").innerHTML = "" + xmlhttp.readyState + " " + xmlhttp.status;
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("postevent").innerHTML = "success response";
            document.getElementById("postevent").innerHTML = "this is it" + xmlhttp.responseText;
        }
    }

    var url = "http://simple.ap.ernet.in/api/home/resource.php/resource/node?";
    url += "key=" + document.getElementById("key").value + "&";
    url += "datasetId=" + document.getElementById("datasetId").value + "&";
    url += "localId=" + document.getElementById("localId").value + "&";
    url += "nodeName=" + document.getElementById("nodeName").value + "&";
    url += "nodeDesc=" + document.getElementById("nodeDesc").value + "&";
    url += "lat=" + document.getElementById("lat").value + "&";
    url += "long=" + document.getElementById("long").value;

    document.getElementById("postevent").innerHTML = url;
    xmlhttp.open("POST", url, true);
    xmlhttp.send();
}

解决方案

The only known workaround is to let the browser think there is only one origin. The easiest is to put a proxy on your own server relaying to the other server. If your own server is in Apache, you may use mod_proxy (see this question).

But note that if the other server doesn't allow cross-origin requests, it's probably against its policy to do that. And if it's a big server, it may very well detect it and ban your server's IP.

这篇关于无法从服务器读取JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆