phpjsonajax

Sending url string via AJAX to PHP


I want to send URL's string via PHP (this must return a JSON), but I guess there is an encoding issue. I use file_get_contents("php://input") in my PHP to get the string of this URL, but it's impossible. This code is working when I am sending a JSON to PHP. Is there an encoding problem?

var urlString='https://google.com';

 $.ajax({
    type        : 'POST',
    url         : 'https://example.php',
    //contentType: 'application/json',
    data: urlString ,                    //I want to send this
    dataType: 'json'
    
    });

Solution

  • data for ajax needs to be in json format

    var urlString='https://google.com';
    
    $.ajax({
    type        : 'POST',
    url         : 'https://example.php',
    data: {"url":urlString} ,                 
    dataType: 'json'
    
    });
    

    then in your php use $url = $_POST["url"];