因此,我尝试像这样向url'/?type = 1'发送请求:
a.ajax({
url: '/?type=1',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(b_)
})
数据已正确发送,但无论使用什么URL都发送至基本URL 127.0.0.1。这样可以使用Ajax检索整个首页。
常规标题如下:
Request URL: http://127.0.0.1:4001/EN/
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:4001
Referrer Policy: origin
响应标题为:
Cache-Control: no-cache, must-revalidate
Connection: keep-alive, Keep-Alive
Content-Encoding: gzip
Content-Language: en
Content-Length: 4516
Content-Type: text/html; charset=utf-8
Date: Mon, 24 Jun 2019 11:17:27 GMT
Expires: 0
Keep-Alive: timeout=5, max=95
Last-Modified: Mon, 24 Jun 2019 11:17:27 GMT
Pragma: no-cache
Server: Apache/2.4.39 (Win64)
Vary: Accept-Encoding,User-Agent
X-Content-Type-Options: nosniff
X-TYPO3-Parsetime: 1551ms
X-UA-Compatible: IE=edge
请求标头显示为:
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 31
Content-Type: application/json; charset=UTF-8
DNT: 1
Host: 127.0.0.1:4001
Origin: http://127.0.0.1:4001
Pragma: no-cache
Referer: http://127.0.0.1:4001/
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
X-Requested-With: XMLHttpRequest
如何将请求发送到正确的网址?
答案 0 :(得分:0)
花了几个小时后,事实证明由于某种原因,我必须在URL中包括完整的路径。因此;
private static String buildPattern(String s) {
String pattern = "^[";
for (int i = 0; i < s.length(); i++) {
pattern += s.charAt(i);
}
pattern += "]*$";
return pattern;
}
此外,我必须删除'?'前面的斜杠'/'。使它起作用。
答案 1 :(得分:-1)
尝试以下代码,希望这对您有用。删除 QueryString 参数,就像 POST 类型一样,您无法将 QueryString 中的数据发送到服务器。 / 表示根目录/
a.ajax({
url: '/',
//data: yourJsonObject,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(b_)
})