asp.net mvc中url地址乱码解决

问题:在页面中使用ajax发送请求时,如果url带有中文则为乱码

解决方案:在发送请求时对url进行encode编码,再后台接收的时候进行decode解码

下面贴代码:

页面:

$(function () {
        var img$ = $("img");
        var url = {
            url1:img$.eq(0).attr("src"),
            url2:img$.eq(1).attr("src")
        };
        $("#btnCompare").click(function () {
            $.ajax({
                url: encodeURI('/Home/ComparePhoto?url1='+url.url1+"&url2="+url.url2),
                dataType: 'text',
                type:'POST',
                success: function (data)
                {
                    if (data > 5) {
                        alert(data+":图片不是很相似");
                    } else {
                        alert(data+":图片很相似");
                    }
                }
            });
        });
    })
//后台:
public
ActionResult ComparePhoto(string url1, string url2) { string s1 = Server.MapPath("/") + Server.UrlDecode(url1); string s2 = Server.MapPath("/") + Server.UrlDecode(url2); SimilarPhoto image1 = new SimilarPhoto(s1); SimilarPhoto image2 = new SimilarPhoto(s2); string hash1 = image1.GetHash(); string hash2 = image2.GetHash(); int count = SimilarPhoto.CalcSimilarDegree(hash1, hash2); return Content(count + ""); }
原文地址:https://www.cnblogs.com/4job/p/10942704.html