我们在内联网上有两个网络服务器。服务器A是我的,服务器B正在运行Tableau。
我基本上想要这样做:
var myTicket = what you would get if this ran AS the server:
var tableau_params = { username: 'DistrictDataReader'};
$.ajax({
type: "POST",
url: "http://Dataviz:8080/trusted",
data: tableau_params,
success: function (msg) { I want to set my myTicket to equal what comes back form server B}
});
我尝试了一些将ajax调用放在Javascript中的东西 - 但我确信它确实不是从Web服务器运行 - 而是客户端。我曾尝试过两种类型的ajax调用,它们都给我带来了CORS错误。
<script>
$(function () {
$("#Result").click(function () {
var tableau_params = { username: 'DistrictDataReader'};
var ticket;
$.ajax({
type: "POST",
url: "http://Dataviz:8080/trusted",
data: tableau_params,
// First format attempt
dataType: "html",
success: function (msg) {
ticket = $('#Result').html(msg.d);
}
// Second format attempt
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
ticket = $('#Result').text(msg.d);
}
});
alert(ticket);
});
});
</script>
我也尝试在服务器B上打开CORS以允许任何操作,但鉴于上述两个示例都返回原始错误,我确信这实际上并不是从我的网络服务器运行。
所以我想要另一种方法来做到这一点。我真的只想将我的变量myTicket从POST设置为http://dataviz:8080/trusted,参数集为{username:'DistrictDataReader'}
我该怎么做?
我在RAZOR的一个cshtml页面上。
答案 0 :(得分:0)
如果它仍然不允许跨源,那么你的CORS设置不正确。尽管如此,您还没有提供任何评估信息。
另一种选择是使用JSONP,假设外部站点支持它或者可以支持它。这完全绕过了对CORS的需求。
答案 1 :(得分:0)
想通了:
这是如何处理没有AJax:
@{
string postData = "username=DistrictDataReader";
byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);
var myTicket = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dataviz:8080/trusted");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postData.Length;
// Write the request
Stream outStream = req.GetRequestStream();
outStream.Write(data, 0, data.Length);
outStream.Close();
// Do the request to get the response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader inStream = new StreamReader(res.GetResponseStream());
string resString = inStream.ReadToEnd();
inStream.Close();
myTicket = resString;
}
catch {myTicket = "Ooops!";}
Response.Redirect("http://dataviz:8080/trusted/"+ myTicket +"/views/SBACStateandCounty/VermontMap");
}