简体   繁体   中英

Forbidden 403 while trying to save <><> chracters with Ajax & jQuery using $_POST

i'm trying to $_POST characters to my php file to save in a database the data of a form.

There is my procedure :

$("#save").on("click",function(el){ 
        el.preventDefault();
        var text = "<h1>test</h1><h1>test</h1>";
        jQuery.ajax({
            type: 'POST',
            data: {_text:text},
            url:  'ajax/do_save.php',
            success: function(data) {
                if(data == "0"){
                    console.log("fail");
                }
                else if(data != "0"){
                    //console.log("success");
                }
            }
        });
});

The php file is not the problem, it's really in this ajax call and the tags <>.

When var text = "<> <>" it crash, and i got the following error :

403 (Forbidden)

But when var text = "<> <", everything is ok the post is Successful and i've got no error and the save is well done in the database.

So, what do you guys think it can be? Seriously, I am totally lost, cause this EXACTLY same code in a precedent project and it work's like a charm. I don't know what I can do now.

You need to encode the elements using encodeURI() .

var text = encodeURI("<h1>test</h1><h1>test</h1>");

Then on the backend you'd use urldecode() ;

$html = urldecode($_POST['_text']);

Example


Alternatively you could simple store the encoded string in the database then decrypt it when you need to use it.

It was a server issue. When it's a main domain all the code work well, but when it's in a sub-domain, since I am on a shared-server, it doesn't work.

Hope it can help some one !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM