1

I'm having trouble trying to send data using ajax via GET method, here's my code, but it doesn't work

$("#selectTypeTour").change(function () {
        let type = $(this).val();
        $.ajax({
            url: url,
            type: 'GET',
            data: {type: type},
            success: function (res) {
                console.log(res);
            }
        });
    })

I tried by adding &type after url then it should work, so what's the problem here?Why is the data: {type: type} not being received?

$("#selectTypeTour").change(function () {
        let type = $(this).val();
        $.ajax({
            url: url + '&type=1',
            type: 'GET',
            data: {type: type},
            success: function (res) {
                console.log(res);
            }
        });
    })

below is the processing code on my server


public function getLocation(){
        if(isset($_GET['type'])){
            echo $_GET['type'];
        }
    }

Here is the code in the .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?url=$1

I realized the problem was that if http:\\myurl?type=1 would not get the type value, if http:\\myurl&type=1 would get it, what is the problem?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Khả Nam
  • 33
  • 6
  • 1
    What does your console say? After you've send a request check the *network* tab in your developer tools to see the outgoing request. There you can check if the request is as it should be. And *it doesn't work* is not a question. – Emiel Zuurbier Sep 03 '20 at 10:30
  • Kindly check the link: https://stackoverflow.com/questions/2407653/jquery-ajax-type-get-passing-value-problem – Kamran Allana Sep 03 '20 at 10:32
  • you should either add the parameters to the URL or use the data field, but not both (which is what you are doing in the second code block) – CamiEQ Sep 03 '20 at 10:47
  • "doesn't work" isn't a useful description of the problem, there could be 100 reasons for a fault. The first version you've posted _should_ work. If it doesn't, please do some debugging as noted in the first comment above. If you don't understand the results or know how to resolve it, then please show us what you found. – ADyson Sep 03 '20 at 10:50
  • @EmielZuurbier the network tab says status code = 200 but it still doesn't seem to be working,this is code PHP ``` public function getLocation(){ if(isset($_GET['type'])){ echo $_GET['type']; } }``` – Khả Nam Sep 03 '20 at 12:56
  • @ADyson I discovered where I was wrong, htttp://myurl?type=abc at this path I could not get the value "type", but at this path htttp://myurl&type=abc, there was What should I do now? this is what I write in the .htaccess RewriteRule ^ (. +) $ index.php?url = $ 1 – Khả Nam Sep 03 '20 at 13:03
  • I discovered where I was wrong, htttp://myurl?type=abc at this path I could not get the value "type", but at this path htttp://myurl&type=abc, there was What should I do now? this is what I write in the .htaccess RewriteRule ^ (. +) $ index.php?url = $ 1, plz help me – Khả Nam Sep 03 '20 at 13:03
  • Your issue seems to be that the PHP file does not receive the request properly. So please add the code responsible for your GET request to your question. – Emiel Zuurbier Sep 03 '20 at 13:05
  • Please add all your extra relevant code and information to the question, using the "edit" button to update your post. Code in comments is not nicely formatted, and therefore is hard to read and understand. Thanks. – ADyson Sep 03 '20 at 13:18
  • Thanks. For the PHP, you say "below is the processing code"...but what's missing from that is how you are executing the `function getLocation() { ... }` function? Have you got a line like `getLocation();` in the same script somewhere? If you don't have that, then the function will never be executed and so the code will never read from the $_GET array. – ADyson Sep 03 '20 at 13:47
  • Anyway your rule `RewriteRule ^(.+)$ index.php?url=$1` is affecting this. What is the intended purpose of this rule? As far as I can see it will change all URLs. So for instance `http:\\myurl?type=1` would become `index.php?url=http://myurl?type=1`. So you can see now that this won't work because there are two `?`s. Whereas `http://myurl&type=1`, which would be incorrect on its own, will be transformed into `index.php?url=http://myurl&type=1` - which works because it separates the parameters with `&`. – ADyson Sep 03 '20 at 13:51
  • So, the rule is causing your issue, but it's unclear why you have got such a rule, or what it's supposed to be used for. Does it have another purpose which is needed in your application? – ADyson Sep 03 '20 at 13:52
  • @ADyson assuming my url is ***http://admin/tour/create*** I use ***RewriteRule ^(.+) $ index.php?url=$1*** to get the url variable out and then parse it into "admin" and "tour" and "create" is in action and method order, how do I edit my .htaccess file now? – Khả Nam Sep 03 '20 at 14:01
  • I[m not an expert on rewrite rules. Perhaps someone else will be able to suggest a way to meet that requirement and also allow querystring parameters to work correctly. You seem to have implemented a very primitive routing engine but I suspect it needs to be more sophisticated. I've added relevant tags to the post so that those with an interest in these topics might see your question. – ADyson Sep 03 '20 at 14:07

0 Answers0