0

This is the first time i am trying to upload my work on a webhost from localhost.I have an ajax request to submit a form and get somre response from the server.But the success method is not triggering ,instead error method is triggering.Though it is working fine on localhost but for some reason it is not working on remote server.I think it is the url in the ajax request which is not getting the file though it is fine on localhost.What might be the reason for this and how i can fix this?

i checked all the sql related with this ajax request.ALl working fine .

my domain name is :ezphp.tk

my question is is attaching the file location in the url is enough like i did or i had to treat it with something like http://mydomain/filepath.....

ajax submission :

 $.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 file is :

 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);

       }
   }
AL-zami
  • 7,637
  • 11
  • 53
  • 104

3 Answers3

0

I think your problem is ajax cross domain. You can set in php file by code:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');  

Or reference here to resolve it

Community
  • 1
  • 1
dieuhd
  • 1,288
  • 1
  • 8
  • 21
0

On your first php page when you call the ajax function try either of these two method:

 <!-- This is your form -->
 <form name="yourForm" id="yourForm" method="post" action="http://yourdomain.com/includes/verifyanswer.php" novalidate >
 <input type="submit"  />
 </form>

This is method to call ajax after form submission:

$('#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');
            }
        });

    }));

This is to call ajax through your custom function:

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');
           }
        });

}

Add this line on top of your php page from where you are getting ajax json data:

// PHP headers (at the top)
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

$output = "Hello world";
echo json_encode($output);
Neeraj Kumar
  • 393
  • 6
  • 16
0

first, you needs to check what exactly error you get from your ajax request, is this caused by cors or something else

change your jquery error function like below:

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

if you using shared hosting for your website, then you need to allow your web is can access from all origin using this, there are several ways to allow cross origin:

  1. in your resuested php file: added header to allow your acces origin using this : header("Access-Control-Allow-Origin: *");
  2. or you can config all your web using .httaccess

for more info about cors enable, you can visit here enable-cors.org