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 31 in JQuery by Edureka
• 13,640 points
29 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 1 by Edureka
• 13,640 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,740 points
25,288 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,740 points
1,377 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,740 points
2,678 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,740 points
2,189 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 in JQuery by gaurav
• 16,820 points
61 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 in JQuery by gaurav
• 16,820 points
74 views
0 votes
1 answer

Ajax using JQuery in ASP .NET c#

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

answered Jun 15 in JQuery by rajatha
• 6,640 points
39 views
0 votes
1 answer

Ajax load issues with space in url

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

answered Jun 10 in JQuery by gaurav
• 16,820 points
89 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 in JQuery by Edureka
• 13,640 points
62 views
0 votes
1 answer

Telerik UI controls vs client-side UI with jQuery

I've used Telerik and JQuery for years. ...READ MORE

answered Jun 1 in JQuery by Edureka
• 13,640 points
18 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP