Solved

Jquery's $.get changes my character encoding

Posted on 2010-08-30
8
651 Views
Last Modified: 2012-05-10
I am using $.get to send some variables to an asp file that saves them to an sql table. When I use a regular form in a separate window special characters (áéíóúñ) are saved correctly but when I use ajax to do so the encoding used is UTF-8 which saves some weird characters instead of the special characters. Any ideas? I need ISO-8859-1!

0
Comment
Question by:robrodp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33563599
Use $.ajax and beforeSend(XMLHttpRequest) :

http://api.jquery.com/jQuery.ajax/
$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  beforeSend: function(req) {
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
  }
});

Open in new window

0
 

Author Comment

by:robrodp
ID: 33563813
Sorry, but I'm new to jquery, how would you write this $.get in terms of a $.ajax?
$.get("submit.asp", { var1: 'var1', var2: 'var2' },
function(data){
function1(data);
function2(data);
function3(data);
...
functionN(data);
});

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33563834

$.ajax({
  url: "submit.asp",
  data: { var1: 'var1', var2: 'var2' },
  success: function(data){
              function1(data);
              function2(data);
              function3(data);
              ...
              functionN(data);
  },
  beforeSend: function(req) {
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
  }
});

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:robrodp
ID: 33572500
The code produces the exact same problem :(
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33572566
Do you have a live link ?
0
 

Author Comment

by:robrodp
ID: 33572622
It's being programmed in the intranet so, sorry no. But I have solved it using htmlEncode, a function that encodes on the client side (Javascript: http://www.tumuski.com/code/htmlencode/) and decodes on the server side (ASP: http://www.psacake.com/web/func/htmldecode_function.htm). Seems to work fine with the original $.get and the $.ajax sequence

Thanks for the support!!
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33572652
You're welcome!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33572668
Thanks for the points!
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question