目录
理论
跨域默认情况下允许方法:GET、HEAD、POST
默认情况下允许的Content-Type:text/plain、multipart/form-data、application/x-www-form-urlencoded
如果要使用用户自定义头,要在被请求的服务器HTTP响应消息中加入:
Access-Control-Allow-Headers: X-Test-Cors
这个X-Test-Cors就是自定义消息头
如果要添加新的跨域方法要添加如下消息:
Access-COntrol-Allow-Methods: PUT, Delete
如果要设置Option这个预请求时间,则要加如下消息头:
Access-Control-Max-Age: 1000
演示及代码
现在有2个页面,都在127.0.0.1的机器上,
一个服务是80端口,一个服务是81端口,
80端口通过JavaScript获取81端口跨域服务!
现在使用自定义消息头!
程序运行截图如下:
此时在81上添加允许X-Test-Cors的响应消息:
运行这个时候就没有报错了,并且浏览器有预处理和响应了!
看下这Option预处理头和Post请求:
这里通过设置Max-Age设置预处理头:
运行截图如下:
程序结构如下:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QTcpServer;
class QTcpSocket;
QT_END_NAMESPACE
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected slots:
void newConnectionSlot80();
void newConnectionSlot81();
void errorStringSlot80();
void errorStringSlot81();
void sendMsg80();
void sendMsg81();
private:
Ui::Widget *ui;
QTcpServer *m_tcpServer80;
QTcpSocket *m_tcpSocket80;
QTcpServer *m_tcpServer81;
QTcpSocket *m_tcpSocket81;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_tcpServer80 = new QTcpServer(this);
m_tcpServer80->listen(QHostAddress::Any, 80);
m_tcpServer81 = new QTcpServer(this);
m_tcpServer81->listen(QHostAddress::Any, 81);
connect(m_tcpServer80, SIGNAL(newConnection()), this, SLOT(newConnectionSlot80()));
connect(m_tcpServer81, SIGNAL(newConnection()), this, SLOT(newConnectionSlot81()));
connect(m_tcpServer80, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot80()));
connect(m_tcpServer81, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot81()));
}
Widget::~Widget()
{
delete ui;
m_tcpServer80->close();
m_tcpServer81->close();
}
void Widget::newConnectionSlot80()
{
qDebug() << "newConnectionSlot80() called";
m_tcpSocket80 = m_tcpServer80->nextPendingConnection();
connect(m_tcpSocket80, SIGNAL(readyRead()), this, SLOT(sendMsg80()));
}
void Widget::newConnectionSlot81()
{
qDebug() << "newConnectionSlot81() called";
m_tcpSocket81 = m_tcpServer81->nextPendingConnection();
connect(m_tcpSocket81, SIGNAL(readyRead()), this, SLOT(sendMsg81()));
}
void Widget::errorStringSlot80()
{
qDebug() << m_tcpServer80->errorString();
}
void Widget::errorStringSlot81()
{
qDebug() << m_tcpServer81->errorString();
}
void Widget::sendMsg80()
{
// QString contentStr = "<html>"
// "<head>"
// "<title>"
// "Socket 80"
// "</title>"
// "</head>"
// "<body>"
// "<h1>Socket 80</h1>"
// "<script src=\"http://127.0.0.1:81\"></script>"
// "</body>"
// "</html>";
QString contentStr = "<html>"
"<head>"
"<title>"
"Socket 80"
"</title>"
"</head>"
"<body>"
"<h1>Socket 80</h1>"
"<script>"
" fetch('http://127.0.0.1:81',{\n"
" method: 'POST',\n"
" headers:{\n"
" 'X-Test-Cors': '123'\n"
" }\n"
" })\n"
"</script>"
"</body>"
"</html>";
//send msg
QString str = "HTTP/1.1 200 OK\r\n";
str.append("Server:nginx\r\n");
str.append("Content-Type:text/html\r\n");
str.append("Connection:keep-alive\r\n");
str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));
str.append(contentStr);
qDebug() << str;
m_tcpSocket80->write(str.toStdString().c_str());
}
void Widget::sendMsg81()
{
QString contentStr = "<html>"
"<head>"
"<title>"
"Socket 81"
"</title>"
"</head>"
"<body>"
"<h1>Socket 81</h1>"
"</body>"
"</html>";
//send msg
QString str = "HTTP/1.1 200 OK\r\n";
str.append("Server:nginx\r\n");
str.append("Content-Type:text/html\r\n");
str.append("Connection:keep-alive\r\n");
str.append("Access-Control-Allow-Origin: *\r\n");
str.append("Access-Control-Allow-Headers: X-Test-Cors\r\n");
str.append("Access-Control-Max-Age: 1000\r\n");
str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));
str.append(contentStr);
qDebug() << str;
m_tcpSocket81->write(str.toStdString().c_str());
}
程序打包下载: