Jquery Ajax URL Error

I am trying to create a messaging system for my website, but I cannot start my ajax. Therefore, I am making a simpler version of the interaction between files.

Here is my file and test.php and load.php are in the root folder (public_html).

I have ajax function in test.php. load.php just echoes wow.

$("#test").click(function(){ alert("Clicked."); $.ajax({ url:"/load.php", type:"POST", beforeSend: function(){ alert("testing"); }, success:function(result){ $("#result").html(result); alert(" sss "+result); } }).error(function(){alert("wrong");}); }); 

Now it works great.

........... how to set the relative path ...

Here is a more complex design

3 files, all in a different folder:

  • messages.php (as root)
    • show all messages
  • control.php (root / panels)
    • the panel will be included in messages.php
  • load.php (root / functions)
    • control.php will use ajax to call and then display the result in control.php

so when the user loads in messages.php, he will load control.php and then run ajax call control.php.

I am so confused about how to configure these paths for ajax
(including control.php in .php posts works fine)

thanks

+6
source share
2 answers

If the files you are trying to make contact with are located in the root directory, you can use / /[file].php so that no matter which page on your path is correct. It looks like you have a relative path problem. Are you getting any errors in the console?

+4
source

To understand the ajax url, I have two things that should always be remembered.

1. If we put slash at the beginning of the ajax url, the ajax url pattern will be `hostname / yourFile` Example:

 // current url: http://sample.com/users // ajax code load from users page $.ajax({ url: '/yourFile.php', ... }); // ajax url will be: http://sample.com/yourFile.php 

2. If we do not use the slash at the beginning, ajax url will add to the current URL in the browser. Example:

 // current url: http://sample.com/users // ajax code load from users page $.ajax({ url: 'yourFile.php', ... }); //...ajax url will be http://simple.com/users/yourFile.php 

I hope this helps people who want to use ajax. Happy coding, thanks.

+28
source

Source: https://habr.com/ru/post/971904/


All Articles