简体   繁体   English

使用json和XMLHttpRequest JavaScript的Web服务

[英]web service using json and XMLHttpRequest javascript

I am developing a chrome extension, and I want to be able to control it's function using key authentication. 我正在开发chrome扩展程序,并且希望能够使用密钥身份验证来控制其功能。 Each key will dispatch json to be sent to the browser using JavaScript. 每个键都将分派json,以使用JavaScript发送到浏览器。 I am stuck because of the same origin policy. 由于相同的原产地政策,我陷入困境。 What is my best option to be able to parse this json data from the chrome extension and still retain security? 我能从chrome扩展程序解析此json数据并保持安全性的最佳选择是什么?

json data json资料

{"valid":"true","info":{"id":"15","username":"johndoe","expire":"1340470800"}}

browser request using javascript 使用javascript的浏览器请求

var xmlhttp;
function loadXMLDoc(url, cfunc) {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = cfunc;
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

loadXMLDoc("http://website.com/user_data.php?key=3455-2534-7765-2335&username=johndoe", function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var json = xmlhttp.responseText;
      alert(json);
  }
});

I think Chrome and Firefox support Cross-Origin Resource Sharing . 我认为Chrome和Firefox支持跨域资源共享

Read about the Access-Control-Allow-Origin HTTP header on MDN and W3C . 阅读有关MDNW3C上的Access-Control-Allow-Origin HTTP标头的信息。

Since you are developing a browser extension, you might want to set: Access-Control-Allow-Origin: * 由于您正在开发浏览器扩展,因此可能需要设置: Access-Control-Allow-Origin: *

Sending headers with PHP is done with the header function. 使用PHP发送标头是通过标头功能完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM