[英]jquery ajax request not working on remote server
这是我第一次尝试从本地主机上将我的工作上传到Web主机上。我有一个Ajax请求来提交表单并从服务器获取更详细的响应。但是成功方法 没有触发,而是错误方法触发了。它在localhost
上工作正常,但由于某种原因在remote server
上不工作。我认为这是ajax请求中的url,尽管在localhost上很好,但没有获取文件。这可能是什么原因以及如何我可以解决这个问题吗?
我检查了与此Ajax request.ALl相关的所有sql正常工作。
我的域名是:ezphp.tk
我的问题是在url中附加文件位置就足够了,或者我必须用http:// mydomain / filepath .....来对待它。
Ajax提交:
$.ajax('../includes/verifyanswer.php',{
data:{
'answer_body': CKEDITOR.instances.content.getData(),
'userpost_post_id': <?php echo $postid;?>,
'users_user_id': <?php echo $userdata->user_id; ?>
},
type:"POST",
dataType:'json',
success:function(response){
alert('bal');
var obj=response;
alert(obj[0].answer_body);
$('#mainanswer').hide();
$('#answerform').hide();
$('#answerthisquestion').show();
var str="<div class='styleanswer' >"+obj[0]['answer_body']+"</div><div class='customcmntholder'></div><span id='customcomment' class='cmnt' onclick='letmecomment(event);'>Add a Comment...</span><form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='cmntform'> <textarea data-id="+obj[0]['answer_id']+" class='customcmntform' placeholder=' add a comment......' ></textarea></form><hr>";
$('#answerwrapper').append(str);
$('#answerwrapper pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
},
error:function(response){
alert('there are some errors');
}
});
verifyanswer.php文件为:
require_once '../core/init.php';
$answer=$_POST['answer_body'];
$post_id=$_POST['userpost_post_id'];
$answerer=$_POST['users_user_id'];
if(isset($answer,$post_id,$answerer)){
if(!empty($answer) && !empty($post_id) && !empty($answerer)){
$db=DB::getInstance();
$result=$db->post_and_fetch("CALL login.post_and_fetch_ans(?,?,?)",array($answer,$post_id,$answerer))->result();
echo json_encode($result);
}
}
我认为您的问题是ajax跨域。 您可以通过代码在php文件中设置:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
或参考这里解决
在第一个php页面上,当您调用ajax函数时,请尝试以下两种方法之一:
<!-- This is your form -->
<form name="yourForm" id="yourForm" method="post" action="http://yourdomain.com/includes/verifyanswer.php" novalidate >
<input type="submit" />
</form>
这是在表单提交后调用ajax的方法:
$('#yourForm').on('submit',(function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
type:'POST',
url: $(this).attr('action'),
dataType: "json",
data:formData,
processData: false,
success:function(data){
alert(data);
},
error: function(data){
alert('there are some errors');
}
});
}));
这是通过自定义函数调用ajax:
function testFunction()
{
var yourparam1 = "abc";
var yourparam2 = "123";
$.ajax({
type: "POST",
url: "http://yourdomain.com/includes/verifyanswer.php",
dataType: "json",
data: {
param1 : yourparam1,
param2 : yourparam1
},
success: function(data) {
alert(data);
},
error: function(data){
alert('there are some errors');
}
});
}
从获取ajax json数据的位置,在您的php页面顶部添加以下行:
// PHP headers (at the top)
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$output = "Hello world";
echo json_encode($output);
首先,您需要检查从ajax请求中得到的确切错误是由于cors或其他原因引起的
更改您的jquery错误函数,如下所示:
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
如果您为网站使用共享托管,那么您需要允许您的网站可以使用此方法从所有来源访问,有几种方法可以允许跨来源访问:
有关cors enable的更多信息,您可以在此处访问enable-cors.org
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.