简体   繁体   English

使用双引号或单引号在Jquery Ajax调用中发送参数

[英]Sending parameters in Jquery Ajax call with quotes, double or single

I am unable to send data through my ajax call if the user put quotes in the textarea. 如果用户在文本区域中加引号,则无法通过ajax调用发送数据。 Is there a way to send text with quotes? 有没有一种方法可以发送带引号的文本? I dont want to get rid of the quotes. 我不想摆脱报价。 I guess this is usually not a big problem, i found very little online about this situation. 我想这通常不是什么大问题,我在网上发现的情况很少。

var description = $('#Description').val();
var title = $('#Title').val();
var parameters = '{content:' + $('#ContentCheck').is(':checked') +
                        ',title: "' + title + '",desciption:"' + description + '"}';

Checkout JSON.stringify(object) which is built into javascript. 检出javascript中内置的JSON.stringify(object)

The jist is, you create a javascript object, and call stringify to create a JSON string. 首先,您创建一个javascript对象,然后调用stringify创建一个JSON字符串。 With your information given above, you might do: 根据上面提供的信息,您可以执行以下操作:

var jsonText = JSON.stringify({
    'content': $('$('#ContentCheck').is(':checked'),
    'title':   title,
    'description': description
});

Here we define a javascript hash using the curly braces, and then pass it to stringify. 在这里,我们使用花括号定义了一个JavaScript哈希,然后将其传递给stringify。

same problem was with me.I tried this and completely working 我也遇到了同样的问题。

 xmlhttp.open("POST","test_test.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    xmlhttp.send("name="+encodeURIComponent(name)+"&details="+encodeURIComponent(details)+"&r_price="+r_price+"&o_price="+o_price+"&id="+id);

and use urldecode($_REQUEST['details'])` when inserting. 并在插入时使用urldecode($ _ REQUEST ['details'])`。

You can convert parameters into a map -- basically an associative array -- instead of a string. 您可以将参数转换为映射(基本上是一个关联数组)而不是字符串。 That way, you can do string manipulation in the backend .NET without any changes in your frontend js; 这样,您可以在后端.NET中进行字符串操作,而无需对前端js进行任何更改。 eg 例如

var url = 'http://path/to/your/backend/script';
var parameters = {
    contentCheck: $('#ContentCheck').is(':checked'),
    title: title,
    description: description
};
$.post(url, parameters);

You can encode quotes using js and decode it server side to get the original string 您可以使用js编码引号,并在服务器端对其进行解码以获取原始字符串

//js // js

function totalEncode(s){
str= s.replace(/\&/g,'&');
str= str.replace(/</g,'&lt;');
str= str.replace(/>/g,'&gt;');
str= str.replace(/\"/g,'&quot;');
str= str.replace(/\'/g,'&#039;');
return(str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM