gpt4 book ai didi

javascript - 将 PHP cURL 转换为 JavaScript(连接到 WCF 服务)

转载 作者:行者123 更新时间:2023-12-03 11:29:51 25 4
gpt4 key购买 nike

我有一个必须连接到的 WCF API。我实际上无权访问其代码,但这有效:

$user = "login";

$password = "pwdstr";
$service_credentials = "external_api_user:sometext";

$arg = $user.'|'.sha1($user."somestr".$password);
$url='https://mysteryUrl.svc/projectsapi/' . $arg;

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_HTTPHEADER => array("Authorization: " . base64_encode("Basic " + service_credentials))));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo $resp;

我希望我的网页能够从相同的 api 获取数据。到目前为止,我已经做到了这一点:

function requestFromApi() {
var user = "login";
var password = "pwdstr";
var service_credentials = "external_api_user:sometext";
var arg = user + '|' + sha1(user + "somestr" + password);
var url = 'https://mysteryUrl.svc/projectsapi/' + arg;

$.ajax({
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", base64_encode("Basic " + service_credentials));
xhr.setRequestHeader("User-Agent",'Codular Sample cURL Request');
},
type: 'GET',
dataType: 'json',
contentType: 'application/json',
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
};

这当然行不通。我对网络开发完全陌生,所以任何帮助将不胜感激。

加载页面时,Chrome 控制台会抛出此错误:

Refused to set unsafe header "User-Agent" 
XMLHttpRequest cannot load http s://verylongUrlAsInTheCodeAbove. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400.

如何设置标题?在我看来,至少问题之一是尝试访问不同的域,那么我该怎么做呢?

还有其他方法可以使用 JavaScript 访问此 API 吗?可以制作一个基本上只是重新发送我的数据的愚蠢的 PHP“API”吗?我愿意接受建议。提前致谢!

编辑:我使用 JSONP:

 <head>

<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.5/jquery.mobile-1.4.5.js"></script>
<script type="text/javascript" src="js/functions.js"></script>

<script>
alert('1');
$(document).ready(function requestFromApi() {
var user = "login";
var password = "pwdstr";
var service_credentials = "external_api_user:sometext";
var arg = user + '|' + sha1(user + "somestr" + password);
var url='https://mysteryUrl.svc/projectsapi/' + arg;

$.ajax({
url: url+"?callback=?",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", base64_encode("Basic " + service_credentials) );
xhr.setRequestHeader("User-Agent",'Codular Sample cURL Request');
},
type: 'GET',
dataType: 'json',
contentType: 'application/json',
success: function (dataWeGotViaJsonp) {
alert(dataWeGotViaJsonp);
},
error: function(){
alert("Cannot get data");
}
});

});
alert('2');
</script>

</head>

现在我收到错误请求错误。那么错误可能出在哪里呢?

最佳答案

您的路径正确,您收到的错误是因为您的 WPF 服务器没有 CORS启用 header 后,CORS header 告诉网络浏览器允许哪些域连接到服务器,如果当前域不允许,浏览器将拒绝连接。

有关如何在 WPF 上启用 CORS 的示例:http://enable-cors.org/server_wcf.html .

关于javascript - 将 PHP cURL 转换为 JavaScript(连接到 WCF 服务),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782120/

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