1

I have:

call.php:

function ajaxnotify(){
   notify('hello');
}
function notify($a){
echo "
    <script src=\"/js/jquery.min.js\"></script>
    <script src=\"//cdn.socket.io/socket.io-1.4.5.js\"></script>
    <script>
    $(function(){
        socket = io.connect(\"MY IP AND PORT\",{secure: true});
        socket.emit('say', {'msg': '$a'})
    });
    </script>
    ";
}
switch($_GET["do"]){
case "1":
     ajaxnotify();
     break;
case "2":
     notify();
     break;
}

and this code absolutely works when I directly go to mysite/call.php. But when I use:

$.ajax({
            type: "POST",
            url: "call.php?do=1",
            data: "",
            success: function(html){

            }
        });

It just doesn't work. it works when I'm directly going to page, but it doesn't work when I call ajax function from html.
Where is my mistake, guys?

1
  • When you're passing query string do then use type "GET" else pass in data: {do=1} Commented Jul 4, 2017 at 7:09

1 Answer 1

6

You're sending the request using POST, yet using $_GET to retrieve the variable in PHP.

Change the type: "POST" in jQuery, to type: 'GET'. Here's a complete example:

$.ajax({
  type: 'GET',
  url: 'call.php',
  data: { do = 1 },
  success: function(html) {
    console.log(html); // to at least verify the response
  }
});
Sign up to request clarification or add additional context in comments.

6 Comments

only for changing this he can not get result bez he write jquery code on request page just see .... we can't do this ..... for resolving this he should be call some other way or send request same page
m not taking about html , it's for jquery see call.php he just don't aware about php and jquery
@NavnitMishra you're not right, my friend, I'm coding about 9 years
oky then try m not as long duration but m sure on the request page we can't run jquery .....
@NavnitMishra I've stripped other part of html for ease
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.