I
I
Iliana2020-05-12 23:30:20
JavaScript
Iliana, 2020-05-12 23:30:20

How to replace parameters in ajax request?

I am creating a service in which it is necessary to show the available quantity of goods on my website using the API request, the script looks like this:

<script>
        $(document).ready(function(){
            let api_link = 'http://simsms.org/priemnik.php?metod=get_count_new&service=opt28&apikey=CRk&country=RU';
            $.ajax({
                method: 'POST',
                url: 'getContentFromUrl.php',
                data: {
                    'ext_url': api_link,
                },
                success: function(data) {
                    const new_data = JSON.parse(data);
                    $('.response').text(new_data.online);
                    console.log(new_data.online);
                },
                error:function() {
                    console.log('Error');
                }
            });
        })
    </script>

The getContentFromUrl file contains the following code:
<?php
$ext_url = $_POST['ext_url'];
echo file_get_contents($ext_url);
?>

So far, the output of the requested value goes to The
question arose: in the link simsms.org/priemnik.php?method=get_count_new&servic... there are two parameters: country and service , and I would like to make it possible to somehow loop through all the services, there are more than a hundred of them, it’s somehow not very good to paint manually, the country parameter does not change in this case.
The problem is that the services are written as opt28, and the numbers are not ordered, they are scattered.

And then the possibility is also of interest: it turns out that the user can select a country (country parameter), and for each new country a different amount of goods of a certain service, it turns out that when the user changes the country, requests for new quantities of goods should be sent in the background, hence the question:
how to do ajax request that would work on a click on my input type="radio" (indicating the country), that is, the user changes the country, and ajax requests are called for all services, and the current values ​​\u200b\u200bthat were displayed by the request indicated above are replayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2020-05-13
@matios

1. This js is passed into the function
2. On the onChange event for radio broadcasting this function.
3. Inside the function, get the value of the selected radio, and even better, pass the selected option as an argument to the function.
4. Substitute the selected option in the url via concatenation, for example.
Ps. Never!!! Do not publish API keys on such sites and do not push them to github

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question