[英]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.