Php 无法使用jquery ajax方法进行跨域调用?
当我试图使用wamp从本地主机调用远程Web服务器上的php文件时,下面的JQuery Ajax方法不起作用。但是,如果它们都在同一个Web服务器上,则可以正常工作。我相信我已经打开了跨域,但仍然无法进行跨域呼叫Php 无法使用jquery ajax方法进行跨域调用?,php,jquery,Php,Jquery,当我试图使用wamp从本地主机调用远程Web服务器上的php文件时,下面的JQuery Ajax方法不起作用。但是,如果它们都在同一个Web服务器上,则可以正常工作。我相信我已经打开了跨域,但仍然无法进行跨域呼叫 <script> $(function() { $("#callAjax").click(function() { var theName = $.trim($("#th
<script>
$(function() {
$("#callAjax").click(function() {
var theName = $.trim($("#theName").val());
if(theName.length > 0)
{
$.ajax({
type: "GET",
url: "http://www.bcbustransit.uni.me/callajax.php",
data: ({name: theName}),
crossDomain: true,
cache: false,
dataType: "text",
success: onSuccess
});
}
});
$("#resultLog").ajaxError(function(event, request, settings, exception) {
$("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
});
function onSuccess(data)
{
$("#resultLog").html("Result: " + data);
}
});
</script>
$(函数(){
$(“#callAjax”)。单击(函数(){
var theName=$.trim($(“#theName”).val();
如果(name.length>0)
{
$.ajax({
键入:“获取”,
url:“http://www.bcbustransit.uni.me/callajax.php",
数据:({name:theName}),
跨域:是的,
cache:false,
数据类型:“文本”,
成功:成功
});
}
});
$(“#resultLog”).ajaxError(函数(事件、请求、设置、异常){
$(“#resultLog”).html(“调用错误:“+settings.url+”
HTTP代码:“+request.status”);
});
函数onSuccess(数据)
{
$(“#resultLog”).html(“结果:+数据);
}
});
您应该看看
在您的情况下,只需在服务器端添加带有响应的
accesscontrolalloworigin::
头即可。请注意,您应该只使用受信任的域,而不是*
。我尝试将数据类型更改为jsoup以及json,但没有成功?除非您在服务器上启用跨源(CORS)支持,您必须切换到JSONP并更改您的服务器以支持JSONP使用的回调,并将结果打包到该回调中。您好Castt感谢您的响应添加访问控制允许源代码:*到我的php文件工作正常,但正如您所说,我需要放置可信域。这对我的应用程序有什么作用,因为我要将我的html文件转换为.apk for andriod应用程序,然后在市场上分发它?这不应该是一个问题,只要你在标题中只指定你的可信服务器。这样,您的应用程序将非常安全,并且将受到XSS注入的保护。如上所述,您也可以考虑JSONP。
<?php
$con=mysqli_connect("freehosting","xyz","xyz","xyz","3306");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"u197344625_cfv");
$result = mysqli_query($con,"SELECT * FROM cfv_businfofull WHERE busnumber = 1 ");
echo "<table border='1'>
<tr>
<th>Bus Number</th>
<th>StopNames</th>
<th>Time</th>
<th>Day Of Week </th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['BusNumber'] . "</td>";
echo "<td>" . $row['StopNames'] . "</td>";
echo "<td>" . $row['Timings'] . "</td>";
echo "<td>" . $row['DayOfWeek'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>