Problem when sending Json data using AJAX

0

Greetings, I'm starting to use ajax technology to send data JSON type to a php sheet to make an insert to the database, in a facebook login the code works perfect unless you show me error 404 not found if I use this code js from an ibicacion in a different directory "dominio.com/ofertas/descuento" for example, now well: I have my JS that is activated from the index, and sends the data to /facebook/facebookusers.php and registers but what happens if the user is in another part of the page, in another directory, let's say it is in offers / deals, ... there the route that I have in my $ .POST of AJAX does not work for me

QUESTION, there is a method or way to use a "generic" location so that from any location on the page the $ .POST is sent correctly

  function saveUserData(userData){
$.post('facebook/facebook-user.php', {oauth_provider:'facebook',userData: JSON.stringify(userData)}, function(data){ return true; }).done(function(data){
    reloadPage();
});
console.log(userData);

}

the code works fine if I run from index.php but if the location changes, that is from another location example: ofertas.php I want to login, I can not do it, error 404 not found. since the route does not match

    
asked by Alvaro Santafe 28.11.2017 в 16:40
source

2 answers

1

Even though it is not a good practice, you can put the complete route.

example: if what you need is in a path like this = home-> bookstores-> facebook- > facebook-user.php

what you can do is put for example: "../ libraries / facebook / facebook-user.php"

the points what they do is to return levels between the folders / locations to be able to put a different route.

    
answered by 28.11.2017 в 16:54
0

The short answer is YES, IT EXISTS the long answer contains a couple of options:

  • Absolute paths : You can use absolute paths and put direct $.post('https://www.midominio.com/carpeta/facebook/facebook-user.php', . This has disadvantages, mainly that in test and production environments the URLs probably change (although there are ways to solve it as by modifying your domain name resolution file). Additionally, the route scheme is fixed: http vs https . Normally you want your links to be independent of this.

  • Routes related to the root of the site : Routes related to the root of the site are obtained with a single point as a prefix: ./ ; you can use it in the following way: $.post('./carpeta/facebook/facebook-user.php and it is equivalent to the link of the previous option.

When you use as a prefix two points ../ what you say to your browser is that the root of your route is the directory where the script is running and that starting from there you have to download n directories for later look for the route that suffixes to these points.

    
answered by 28.11.2017 в 17:52