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

javascript - Uncaught Error: INVALID_CHARACTER_ERR: DOM Exception 5 - Stack Overflow

programmeradmin2浏览0评论
$('form#update').submit(function(){
    var _data = $(this).serializeArray();
    var param = {};
    $.map(_data,function(a,b){
        if(a.name=='HotelName'){
            param[a.name] = window.btoa(a.value);
        }
        else{
            param[a.name] = a.value;
        }
    });
    console.log(param);
    $.post('api.coupon_edit_post.php',param,function(r){
        coupons();
        reloadMarkers();
    });
    return false;
});

Is window.btoa causing this error?

I'm using window.btoa so I can pass most of the characters.

EDIT: I tried the MDN solution but it's not working. I am using Google Chrome Version 24.0.1312.57 m

$('form#update').submit(function(){
    var _data = $(this).serializeArray();
    var param = {};
    $.map(_data,function(a,b){
        if(a.name=='HotelName'){
            param[a.name] = window.btoa(a.value);
        }
        else{
            param[a.name] = a.value;
        }
    });
    console.log(param);
    $.post('api.coupon_edit_post.php',param,function(r){
        coupons();
        reloadMarkers();
    });
    return false;
});

Is window.btoa causing this error?

I'm using window.btoa so I can pass most of the characters.

EDIT: I tried the MDN solution but it's not working. I am using Google Chrome Version 24.0.1312.57 m

Share Improve this question edited Feb 13, 2013 at 10:25 Christoph 51.3k21 gold badges101 silver badges128 bronze badges asked Feb 12, 2013 at 9:32 hodlhodl 1,42012 silver badges22 bronze badges 7
  • In which line did this error occur and on which browser? – Uooo Commented Feb 12, 2013 at 9:51
  • @w4rumy no line log indicated. I'm just using GC console. – hodl Commented Feb 12, 2013 at 10:01
  • The way to decide whether window.btoa causes this is to create a test case with just the call to window.btoa and the piece of data contained in a.value. – Álvaro González Commented Feb 12, 2013 at 10:02
  • 1 Enable Pause on uncaught exceptions in the Chrome debugger. – Barmar Commented Feb 12, 2013 at 10:02
  • @ÁlvaroG.Vicario What did you edit on my question? I see nothing. Regarding your advice. I think it so because when I removed that line, it's fine. – hodl Commented Feb 12, 2013 at 10:08
 |  Show 2 more ments

1 Answer 1

Reset to default 6

The Error es definitely from window.btoa.

You should modify the MDN solution and omit the decoding step before the conversion, like following:

function utf8_to_b64( str ) {
    return window.btoa(encodeURIComponent( str ));
}

function b64_to_utf8( str ) {
    return decodeURIComponent(window.atob( str ));
}

This creates the b64 string from the encoded string instead of decoding it again (which again would create symbols, btoa can't process). Now this works:

utf8_to_b64('✓ à la mode');
b64_to_utf8("JUUyJTlDJTkzJTIwJUMzJUEwJTIwbGElMjBtb2Rl");

of course due to the encoding this significantly lengthens the b64 string.

You can now modify your example:

param[a.name] = utf8_to_b64(a.value);
发布评论

评论列表(0)

  1. 暂无评论