0

好吧,我正在尝试将一些数据发布到某个站点以便 g,但是 javascript 似乎不太喜欢我:(。要执行我的请求:

  var xmlhttp;
    if (window.XMLHttpRequest)
    xmlhttp=new XMLHttpRequest();
    else
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("POST","http://(removed)/forums/en/shoutbox_comet.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=utf-8");
    xmlhttp.setRequestHeader("X-Request","JSON");
    xmlhttp.setRequestHeader("Cookie",document.cookie); 
    xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4)
    alert(xmlhttp.responseText);
    }
    var channels = ['english_486', 'english_0', 'english_459','english_293','english_1310','english_292','english_459','english_293','english_970'];
    var channel = channels[Math.floor(Math.random() * channels.length)];
    var data = "channel="+channel+"&action=publish&type=message&data=HelloWorld";
    xmlhttp.send(data);

发送这个:

OPTIONS http://(removed)/forums/en/shoutbox_comet.php HTTP/1.1
Host: (removed)
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://(removed)
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-request
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

我怎样才能解决这个问题?我希望它发送:

POST http://(removed)/forums/en/shoutbox_comet.php HTTP/1.1
Host: (removed)
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
X-Request: JSON
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Referer: http://(removed)/forums/en/shoutbox_comet_standalone.php?f=293&style=popout
Content-Length: 55
Cookie: (removed)
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

channel=english_293&action=publish&type=message&data=HelloWorld
4

1 回答 1

1

您正在向其他域发出请求。这是浏览器的安全问题。输入CORS。浏览器在 POST 之前发送一个 OPTIONS 请求(有时称为“预检”请求),以确定服务器是否可以响应它。

您必须确保您的服务器正确响应这些请求。

于 2013-09-06T15:51:53.043 回答