gpt4 book ai didi

javascript - ajax的授权和cors的二次响应如何显示内容?

转载 作者:可可西里 更新时间:2023-11-01 02:09:07 24 4
gpt4 key购买 nike

我已经在 vps 中构建了基本的授权和 cors。

curl -X 选项 -i http://111.111.111.111

HTTP/1.1 200 OK
Date: Sat, 15 Sep 2018 08:07:37 GMT
Server: Apache/2.4.6 (CentOS)
Access-Control-Allow-Origin: http://127.0.0.1
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
Allow: OPTIONS,GET,HEAD,POST,TRACE
Content-Length: 0
Content-Type: httpd/unix-directory

curl -u xxxx:xxxx -i http://111.111.111.111/remote.php

HTTP/1.1 200 OK
Date: Sat, 15 Sep 2018 08:08:07 GMT
Server: Apache/2.4.6 (CentOS)
Access-Control-Allow-Origin: http://127.0.0.1
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
Last-Modified: Sat, 15 Sep 2018 07:54:13 GMT
ETag: "24-575e43f02c324"
Accept-Ranges: bytes
Content-Length: 36


<?php
echo '{"name","myname"}';
?>

可以看到authorization和cors状态都很好。

我本地目录 /var/www/html 中的 test-ajax-cors.html。

<script src="http://127.0.0.1/jquery-3.3.1.js"></script>
<script>
function Ajax( ) {
var url = 'http://111.111.111.111/remote.php';
$.ajax(url, {
type:"post",
crossDomain: "true",
dataType:"json",
beforeSend:function(xhr) {
xhr.setRequestHeader('Authorization',"Basic " + btoa("xxxx:xxxx"))},
success:function(response){
data = JSON.stringify(response);
alert(data);
mytext = $("#remote");
mytext.append(data);
},
error: function (e) {
alert("error");
}
});
};
</script>

<input type="button" value="show content" onclick="Ajax();">
<p id="remote">the content on remote webpage</p>

http://111.111.111.111中的remote.php。

cat/var/www/html/remote.php

<?php
echo '{"name","myname"}';
?>

输入127.0.0.1/test-ajax-cors.html,点击show content
1.我收到警报信息:错误
2.remote.php 被 127.0.0.1/test-ajax-cors.html 调用了两次(127.0.0.1/test 中的 show content 按钮-ajax-cors.html 仅被点击一次)。

the first response什么时候第一次调用remote.php,remote.php的resposne没有内容。
第一个请求可能是 CORS 预检请求,浏览器发送一个没有任何授权 header 的 OPTIONS 请求,在我的例子中,服务器向浏览器发送 200 状态代码,这意味着一切都处于良好状态。

enter image description here

第二次调用remote.php时remote.php的响应内容。
如何让127.0.0.1/test-ajax-cors.html显示第二个响应的内容。
感谢 Sally CJ 的通知。

yum install mod_php
systemctl restart httpd

输入127.0.0.1/test-ajax-cors.html,点击show content
1.alert错误信息

enter image description here

2.remote.php被调用了两次,remote.php的响应都是一样的。

{"name","myname"}

enter image description here enter image description here

内容是我所期望的,为什么网页127.0.0.1\test-ajax-cors.html不能显示,导致报错信息?

最佳答案

首先,看起来您的服务器没有正确设置,因为您的 PHP 脚本没有执行并且响应只是返回脚本的内容(我看到您已经解决了这个问题使用 mod_php)。

其次,{"name", "myname"} 不是有效的 JSON。您的响应应该是 {"name": "myname"} 或者为了方便起见,您应该总是 json_encode 一个 PHP 数组,例如:

<?php
// Set the correct Content-Type
// this helps when viewing in browser console, and is generally needed
header('Content-Type: application/json');

// Create an array with needed response data
$result = [
'name' => 'myname'
];

// Convert PHP array to a correct JSON string and echo it
echo json_encode($result);

这样您的 JSON 将始终有效,或者至少您会看到有助于调试的错误。

更正您的回复后,您可以执行以下操作:

success: function(response, textStatus, jqXHR) {
// var stringData = JSON.stringify(response); you don't need this
// since jqXHR object already contains the raw response text

// It's recommended to prefix variable name with $
// that way you will always know that it is a JQuery object
var $mytext = $("#remote");
$mytext.text(jqXHR.responseText);

// console.log(response); is better than alert
// unless you really need to pause the script
alert(response.name);
}

请注意,JQuery 足够智能,可以判断请求是否为 pre-flight 请求,因此您的回调应该仅在第二个请求传送内容时触发.

关于javascript - ajax的授权和cors的二次响应如何显示内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com