2

I have a jquery script that just runs fine on http://mysite.com

and looks like this:

Javascript:

$('#form_changePass').live('submit', function() {
    //alert('form_changePass cliked');
    $.POST("_ajaxCall.php", $(this).serialize(),
    function(data){
        var json = eval('('+data+')');
        //alert(data);    
        if(json.message=='alert') {
            $('div.info').hide();
            $('div.alert').show().html(json.string);
        }
        if(json.message=='info') {
            $('div.alert').hide();
            $('div.info').show().html(json.string);
        }
    }
    );
    return false;
})

PHP script:

if ($_POST['formType']=="form_changePass") {
.....
.......
   $arr = array( 'message' => 'info', 'string' => $INFO['updatePass']);
   echo json_encode($arr);
}

Now when I move the domain into https the jquery script is not working anymore. I do not get any return value back. Nothing happens.

I also tried to change the POST to getJSON in the javascript and changed the $_POST to $_GET in the php script but it is still not working.

Anybody a idea?

mario
  • 177
  • 2
  • 11

2 Answers2

1

http and https are considered different domains, so it is a cross domain issue. The fix is:

header('Access-Control-Allow-Origin: https://domain.com');

Add this to your php script. Or this only in development so you don't need to worry too much:

header('Access-Control-Allow-Origin: *');
Marian Zburlea
  • 9,177
  • 4
  • 31
  • 37
-1

.serialize serializes the form data as a string, so send them using a parameter instead.

$('#form_changePass').on('submit', function() {
    //alert('form_changePass cliked');
    $.POST("_ajaxCall.php", { formData: $(this).serialize() },
    function(data){
        var json = eval('('+data+')');
        //alert(data);    
        if(json.message=='alert') {
            $('div.info').hide();
            $('div.alert').show().html(json.string);
        }
        if(json.message=='info') {
            $('div.alert').hide();
            $('div.info').show().html(json.string);
        }
    }
    );
    return false;
})

And stop using .live() methods, they have been deprecated now. Use .on() methods.

Starx
  • 77,474
  • 47
  • 185
  • 261
  • 3
    Do you have a reputable source for "Its preferred that you send the serialized data using a parameter instead."? – Anthony Grist May 04 '12 at 23:09
  • @AnthonyGrist, Yeah I read an article about it, am trying to find it. If not I will edit the answer :) – Starx May 04 '12 at 23:10
  • What's the point of sending `formData` argument with serialized form data? How it may help to solve the problem? – VisioN May 04 '12 at 23:18
  • @VisioN, I dont understand your concern about the What and How? The point is for the data to be accessible under a variable, and by making it a valid POST request, it solves the problem. – Starx May 04 '12 at 23:21
  • @Starx Please read the [jQuery docs](http://api.jquery.com/jQuery.post/) carefully: _data - a map or **string** that is sent to the server with the request_. And also have a look at the example #4 under the same link. – VisioN May 04 '12 at 23:24
  • @VisioN, I know the point you are trying to make, but I can't find an live code, which is using the OP's method. Can you point me one? – Starx May 04 '12 at 23:31