2

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'
    
    });

3 Answers 3

1

Welcome to SO!

I think the issue is with your formatting of urlString.

Normally you would want to form it with a key and value, such as:

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

The url you are POSTing data to will usually require that key.

Sign up to request clarification or add additional context in comments.

Comments

0

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"];

Comments

0

Try:

$url = file_get_contents('your-url', false);
$json = json_decode($url, true);
$var = implode(" ",$json);    

Comments

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.