How to pass special characters _javascript techniques based on javascript

Source: Internet
Author: User

Let's introduce JavaScript special characters before we get to the right track.

You can use backslashes in JavaScript to add special characters to a text string.

inserting special characters

The backslash is used to insert ellipses, line breaks, quotes, and other special characters in a text string.

Take a look at the following JavaScript code:

var txt= "We are the so-called" Vikings "from the north."
document.write (TXT)

In JavaScript, strings start or end with either single or double quotes. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must precede the quotation mark in "Viking" with a backslash (\). This converts each double quote to a literal string.

var txt= "We are the so-called \ Vikings\" from the north. "
document.write (TXT)

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.

This is another example:

 
 

The above example produces the following output:

You & Me are singing!

The following table lists the remaining special characters that can be added to a text string using a backslash:

Code Output
\' Single quotation mark
\" Double quotes
\& and number
\\ Back slash
\ n Line feed
\ r return character
\ t Tabs
\b Backspace
\f Page breaks

Background:

Today, while doing a task, using jquery Ajax to pass a long string of characters, in the background to verify has been unsuccessful, tangled up when I was (that string is a random generation, expertise). Check the morning, the original is I generated a string in the + number, and in the JS Pass, will be understood to be connected to the character land, To the background will automatically change the + number to space, so the background of the string and foreground generation has been different.

Reason:

JS after the automatic resolution of special characters, such as the + number for the connector, resolved to the space,& as a variable connector, server-side acceptance of data & after the data is not displayed and so on.

Solution:

1, put the characters in the form, and then use JS to submit form form to the server.
2. Replaces special characters in characters with hexadecimal characters, some special characters correspond to hexadecimal:

+ Space / % & = #
%2b %20 %2f %3f %25 %26 &3d %23

str = str.replace (/\+/G,%2B); Replace the + number with hexadecimal

3, the simplest one, using the encodeURIComponent () function.

The method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks:-_. ! ~ * ' ( ) .
Other characters (for example:;/? : @&=+$,# These punctuation marks used to separate URI components are replaced by one or more hexadecimal escape sequences.

JQuery Ajax Special Character parameters

A problem encountered when doing Ajax login, when the incoming parameter contains special characters, such as: "$ ' #@" and so on. There is a problem with parameter passing and cannot get it correctly.

$.ajax ({
  URL: '/user/login.ydd ',
  type: ' Post ',
  data: ' Name=abce&password=abcd&pwd '
  , Success:function (data) {
  }
})

I want to pass in the user name: ABC, password for abcd&pwd user login. However, incoming background fetch parameters are passed by PASSWORD=ABCD as a parameter,& this special will pwd separated as another parameter parsing.

Solution, which requires an Ajax alternative way of passing parameters

$.ajax ({
  URL: '/user/login.ydd ',
  type: ' Post ',
  data:{' name ': ' ABCE ', ' Password ': ' Abcd&pwd '},
  success:function (data) {
 
  }
})
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.