0

Here is my javascript jquery ajax function to send data. Here i append my data with url to send it to server.

 url = "/login/auth/";
        $.ajax({
            dataType: "json",
            url: url+encodeURIComponent("a+b+2"),
            success: function (data, textStatus, jqXHR) {
                if (data.login === false)
                {
                    alert("Invalid Password");
                } else {
                    $('.login').hide();
                    $("body").append(data.html);
                }

            }
        });

In PHP

 if (isset($_GET['r']))
 {
        echo 'r='.$_GET['r'];
        return explode('/',rtrim($_GET['r'], '/'));
    }

PHP decoding it as login/auth/a b 2

4
  • url via ajax request is login/auth/%7B%22pwd%22%3A%22a%2Bb%2B2%22%7D Commented Oct 21, 2015 at 21:56
  • 1
    Where is ?r= in your URL? Commented Oct 21, 2015 at 21:58
  • login/auth/%7B%22pwd%22%3A%22a%2Bb%2B2%22%7D should not result in login/auth/a b 2, even with double decoding. Are you sure you test the right url and you're not getting a page from cache? Commented Oct 21, 2015 at 23:14
  • ?r= is in .htaccess file in server @Barmar Commented Oct 22, 2015 at 10:05

2 Answers 2

1

It's because a + symbol in a url means a space. It's basically a shorter (and more readable) version of %20.

To send a + encode it as you would with any other special character. An encoded + is %2B. But actually, encodeURIComponent should do that already. Did you maybe decode the url twice in PHP?

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

4 Comments

then how do i send + symbol as data to server
See edited answer, but since you already use encodeURIComponent, I'm not sure what could be the problem. Note though, that PHP will automatically decode the url once, so you don't need url_decode in PHP to decode (parts of) your URL yourself. If you need debugging help with the PHP part, please add that code to the question too.
Weird issue. I've tried your code in a small script on my localhost WAMP. With ?r=a+b+c it echoes a b c, but with ?r=a%2Bb%2Bc it echoes a+b+c, like it should. Maybe it's some configuration or a bug, but I can't find any reference of it, and I can't reproduce it.
I suspect the extra decode may be related to the Apache rewrite rule.
0

changing apache rewrite rule flag to [NE,B,L,QSA] solved the problem. Here B(Back Reference) flag is set to avoid decoding of the url component.

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.