最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot send special characters via jQuery ajax - Stack Overflow

programmeradmin3浏览0评论

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). It all works fine when I use non special characters like a-z but fails when using for example åäö.

On the serverside I am using PHP. I print the tag to see if it "arrives" and all a-z works fine but the åäö is not displaying at all. They seem to not "arrive".

What can be wrong?

This is my jQuery code:

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

Using this solved it! Works fine now.

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

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). It all works fine when I use non special characters like a-z but fails when using for example åäö.

On the serverside I am using PHP. I print the tag to see if it "arrives" and all a-z works fine but the åäö is not displaying at all. They seem to not "arrive".

What can be wrong?

This is my jQuery code:

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

Using this solved it! Works fine now.

{tags: encodeURIComponent(tags), limit: limit}
Share Improve this question edited Jan 14, 2012 at 16:46 Jonathan Clark asked Jan 14, 2012 at 16:21 Jonathan ClarkJonathan Clark 20.5k29 gold badges115 silver badges177 bronze badges 3
  • Try "tags=" + encodeURIComponent(tags) + "&limit=" + encodeURIComponent(limit), or {tags: tags, limit: limit}. – Rob W Commented Jan 14, 2012 at 16:24
  • This don´t work either: data: "tags=" + encodeURIComponent(tags) + "&limit=" + limit – Jonathan Clark Commented Jan 14, 2012 at 16:28
  • {tags: tags, limit: limit} does not work either. – Jonathan Clark Commented Jan 14, 2012 at 16:44
Add a comment  | 

3 Answers 3

Reset to default 13

Data (tags) must be encoded before sending it to server using encodeURIComponent()

Below code is working fine for sending & and "" or any special characters via ajax call:

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

You can achieve this by using JSON object.

For example:

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

or, you can use URLencode before post.

发布评论

评论列表(0)

  1. 暂无评论