不同ip地址,不同域名,能通过cors跨域吗?-灵析社区

万码F5GTP6P0

问题是这样的,可能之前问的有点不好,现在修改下。我用wampserver建立两个站点ajax.com和ajax2.com,两个域名指向的ip地址分别为127.0.0.1,127.0.0.2,但端口同为80(不知怎么改为不同的端口),现在我想在ajax.com下的html文件,发送数据,同时请求ajax2.com的php文件内容,数据一起返回。 <script> function createCORSRequest(method, url, data) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { xhr.open(method, url, true); //true指的是异步 } else if (typeof XDomainRequest != "undefined") { xhr = new XDomainRequest(); xhr.open(method, url); } else { xhr = null; } if (method === "post") { xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(data); } else if (method === "get") { xhr.send(null); } return xhr; } var request = createCORSRequest("post", "http://ajax2.com/AJAX/05-XMLHTTPRequest-test.php","name=jack&age=20"); if (request) { request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { console.log(request.responseText); } }; } </script> <?php header("Content-Type:application/json;charset=utf-8"); header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Methods:GET,POST'); header('Access-Control-Allow-Headers:x-requested-with,content-type'); header('Access-Control-Allow-Headers: NCZ'); echo '请求成功'; echo $_POST['name'].' '.$_POST['age']; ?> 这样就算是成功通过cors跨域了吧,然后呢,我是想找个线上的api来测试下,但找不到,能否给个例子看看,感激不尽。还有就是像这种接口这么调用获取数据?网址http://www.bejson.com/knownjs...,例如我想获取京东的数据,该怎么写?

阅读量:216

点赞量:0

问AI
1、你上面所述的情况,的确说明了跨域成功。2、有线上的api,但是很少有直接说明原理的。提供的api是为了方便接入系统的人调用,因此多数都直接写明使用方法。如http://datainfo.duapp.com/sho...3、你提到的bejson就提供api接口调用。如果你想获取京东的商品信息,就像你所提到的那样。 代码部分:把你请求数据的网址换成http://p.3.cn/prices/mgets?sk...,请求模式GET就行。展示信息需要知道数据解析规范。规范是由接口的制定者决定的。