Ajax load issues with space in url

0 votes

I have 2 python files: gui.py & index.py

python.py contains table with records from mysql database.

gui.py contains python.py and textfield with button to send messages.

Also, gui.py contains javascript that will load new python.py with parameters.

gui.py script

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#content").load("http://localhost/index.py");
    $("button").click(function(){
        var url = "http://localhost/index.py?name=1&message=";
        var msg = document.getElementById("message").value;
        var res = url.concat(msg);
        alert(res); //here I always can understand that result URL is okay.
        $("#content").load(res);
    });
</script>

And index.py part

cursor.execute("INSERT INTO `messages` (`sender`, `receiver`, `text`, `time`) VALUES ('" + req.form['name'] + "', '3', '" + req.form['message'] + "', now())")
});

Where req is from def index(req): I am using mod_python.

So, the problem is - when i try to send message with space is in - my table disappears (till next page refresh (but i don't need to refresh it without spaces, ajax load works good here)). And then I will see only the first word of the message. As you can see in my code, i have alert(res); if I copy that alert and just run that url - spaces works nicely.

Some details

I am using ubuntu 14.04, apache2, mod_python, python 2.7, mysql database, python-MySQLdb.

May 27, 2022 in JQuery by Edureka
• 13,690 points
625 views

1 answer to this question.

0 votes

You should URL encode the message body to protect against special characters in the URL, like, say, space. If you did

var msg = encodeURIComponent( document.getElementById("message").value );

You may have better success.

answered Jun 10, 2022 by gaurav
• 23,260 points

Related Questions In JQuery

0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello, Here is the syntax using jQuery $.get $.get(url, data, ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
43,849 views
0 votes
1 answer

How can I select an element with multiple classes in jQuery?

Hello @kartik, If you want to match only ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,840 points
3,446 views
0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello @kartik, Use data option of ajax. You ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,840 points
7,033 views
0 votes
1 answer

How to find a parent with a known class in jQuery?

Hello @kartik, Assuming that this is .d, you can write $(this).closest('.a'); The closest method returns the ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,840 points
7,259 views
0 votes
1 answer

jQuery find all elements with class beneath parent, even in children elements

Remove > from your select $(".parent .searchEl"). You can use the .find() method ...READ MORE

answered Jun 7, 2022 in JQuery by Edureka
• 13,690 points
10,740 views
0 votes
1 answer

Ajax using JQuery in ASP .NET c#

jQuery Ajax in ASP.Net $.ajax({      & ...READ MORE

answered Jun 15, 2022 in JQuery by rajatha
• 7,680 points
4,043 views
0 votes
1 answer

How to shorten repeating code with html function and return tags in jquery

Firstly note that replace() only accepts two arguments, the ...READ MORE

answered Jun 15, 2022 in JQuery by rajatha
• 7,680 points
919 views
0 votes
1 answer

Ajax load issues with space in url

You should URL encode the message body ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,690 points
1,762 views
0 votes
1 answer

What is the difference between AJAX with JavaScript and jQuery?

JavaScript is a programming language. JQuery is ...READ MORE

answered May 30, 2022 in JQuery by gaurav
• 23,260 points
800 views
0 votes
1 answer

Show Page Loading Spinner on Ajax Call in jQuery Mobile

 Use the ajaxStart() and ajaxStop() Method While working ...READ MORE

answered Jun 10, 2022 in JQuery by gaurav
• 23,260 points
1,438 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP