问题描述
我在使用jquery post函数时遇到了一个令人沮丧的问题,这可能是由于不了解其正确工作方式所致.
I am having a rather frustrating problem with the jquery post function that probably stems from not understanding how it works correctly.
我有一个函数,该函数应该将一些表单信息发布到我编写的php脚本中,然后该脚本针对API运行curl请求,以解决javascript的跨域策略.只要它提交到"http",它似乎就可以正常工作,但是当我将其发送到"https"时,表单永远不会提交.
I have a function that should post some form information to a php script that I wrote and that script then runs curl requests against an API to get around the cross-domain policy of javascript. It seems to work fine as long as it submits to "http" but when I send it to "https" the form never gets submitted.
我在计算机上运行了Wireshark,直到我使URL使用http之前,它都未显示出指向目标ip的流量.我在服务器上具有基本的身份验证,因此我通过url传递了用户名和密码,但是在那里没有经过测试,得到了相同的结果.
I ran wireshark on my computer and it showed no traffic towards the destination ip until I made the url use http. I have basic auth on the server so I am passing the user and password through the url, but tested without that there and got the same results.
这是无效代码:
$j.post("https://<api user>:<password>@<ip>:444/ProxyScript.php",
$j("#spoke_ticket").serialize(),
function(msg) {
log_status(msg);
fade_status();
$j(':input','#createtheticket')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
这是工作功能:
$j.post("http://<other ip>/ProxyScript.php",
$j("#spoke_ticket").serialize(),
function(msg) {
log_status(msg);
fade_status();
$j(':input','#createtheticket')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
关于为什么不发送流量的任何想法?让我知道是否遗漏了一些关键信息或其他内容.
Any ideas as to why the traffic is not being sent?Let me know if I left out some key information or anything.
感谢您的帮助
推荐答案
为什么不使用代理来解决跨域问题?听起来更容易.一个简单的例子是,当我想获取有关县,道路名称等的丹麦政府国家地理数据时(对我来说幸运的是,它们的数据是json或XML可选)
Why not use a proxy to get over the cross-domain issue? It sounds more easy. An simple example is when i want to retrieve the danish administration national geo-data for counties,road names and so on (lucky for me, their data is in json or XML optional)
简化的proxy.php
simplified proxy.php
<?
header('Content-type: application/json');
$url=$_GET['url'];
$html=file_get_contents($url);
echo $html;
?>
在ajax中,获取县边界的纬度/经度
in ajax, get the lat/longs for a county borderline
var url= "proxy.php?url=https://geo.oiorest.dk/"+type+"/"+nr+"/graense.json";
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
...
});
请注意https-实际示例中的网址可能是 https://geo.oiorest.dk/kommuner/0810/graense.json
notice the https - the url could be, real example, https://geo.oiorest.dk/kommuner/0810/graense.json
这篇关于AJAX发布不适用于HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!