Solved

Jquery's $.get changes my character encoding

Posted on 2010-08-30
8
639 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
  • 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
 

Author Comment

by:robrodp
ID: 33572500
The code produces the exact same problem :(
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now