简体   繁体   English

无法通过jQuery ajax发送特殊字符

[英]Cannot send special characters via jQuery ajax

I am developing a webpage where user is searching for files using tags. 我正在开发一个网页,用户正在使用标签搜索文件。 I am using jQuery Ajax to do the remote call to the API (database). 我正在使用jQuery Ajax来远程调用API(数据库)。 It all works fine when I use non special characters like az but fails when using for example åäö. 当我使用像az这样的非特殊字符但在使用例如åäö时失败时,一切正常。

On the serverside I am using PHP. 在服务器端我使用PHP。 I print the tag to see if it "arrives" and all az works fine but the åäö is not displaying at all. 我打印标签,看它是否“到达”,所有az工作正常,但åäö根本没有显示。 They seem to not "arrive". 他们似乎没有“到达”。

What can be wrong? 有什么不对?

This is my jQuery code: 这是我的jQuery代码:

var tags = $('#tags').val();

$.ajax ({
    type: "POST",
    url: base_url + "search", 
    data: "tags=" + tags + "&limit=" + limit, 
    beforeSend: function (html) {
            $("#search_results").html("Searching for files...");
    },
    success: function (html) {
            $("#search_results").html(html);
    },
    error: function (html) {
            $("#search_results").html('Something went wrong!');
    }
});

This is my server side code: 这是我的服务器端代码:

echo ($_POST['tags']);

I search and looked at related questions about this here on SO but non helped me unfortunately. 我在这里搜索并查看了有关此问题的相关问题,但遗憾的是没有帮助我。

UPDATE UPDATE

Using this solved it! 用这解决了! Works fine now. 现在工作正常。

{tags: encodeURIComponent(tags), limit: limit}

在使用encodeURIComponent()将数据(标签)发送到服务器之前,必须对其进行编码

Below code is working fine for sending & and "" or any special characters via ajax call: 下面的代码适用于通过ajax调用发送&和“”或任何特殊字符:

specialChar1= "JΛ̊KE#2@#*&($^@%#*@#%))*$&@*(""" ;
specialchar2 ="??&!!--##";
url = "/get/" + encodeURIComponent( specialChar1) +"/"+  encodeURIComponent ( specialchar2 )

You can achieve this by using JSON object. 您可以使用JSON对象来实现此目的。

For example: 例如:

[{"AttributeId":"4035","Value":"Street & House"}]

or, you can use URLencode before post. 或者,您可以在发布之前使用URLencode

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

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