Pregunta

I have task to submit 2 values to a php file using ajax for which I have done the below code in jquery $.POST

$.post(
    "http://localhost/gcm_server_files/sample.php",
     {regId: id,message:msg},
     function(data){
      alert(data);      
    });     

and the code in sample.php is:

echo $_POST['regId'].' AND '.$_POST['message'];

I can see the POST values in console, but cannot see the response..

The values I am posting are:

regID:APA91bGmks8s9ytasjkhdjkhsdjkahsdkjhakjlYkpWpPmjB_nUhbTmVtAho7M6o-W4rsVJorB-ozX9v50YkBMg
message: Testing

Can someone show me whats wrong with code?

¿Fue útil?

Solución

I think the problem is with the url; You are using the absolute path ( http://localhost/gcm_server_files/sample.php) in URL. Try to use the relative path of file in the URL (/sample.php) .

See this for more detail on the same origin policy that applies to Ajax request.

http://en.wikipedia.org/wiki/Same_origin_policy

  Try this 

    $.post(
    "relative_path_to/sample.php",
     {regId: id,message:msg},
     function(data){
      alert(data);      
    });   
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top