Javascript toSource() method not working

前端 未结 5 1762
臣服心动
臣服心动 2020-12-05 18:58

I\'m getting a \"Object doesn\'t support this property or method error\", does anyone know why?

I do have values plugged into userId, fname, lname, oname, sam, hasAc

相关标签:
5条回答
  • 2020-12-05 19:17

    toSource() does not work in Internet Explorer or Safari. It is Gecko-only. See Implementing Mozilla's toSource() method in Internet Explorer for alternatives.

    0 讨论(0)
  • 2020-12-05 19:18

    You can either call toString instead, or put in a condition like this...

    var jsonstuff = (emp.toSource) ? emp.toSource() : emp.toString();
    

    EDIT:

    Since this is not working for you, you might want to use JSON.stringify()

    0 讨论(0)
  • 2020-12-05 19:19

    Whilst not recommended (to extend native JS Objects), during development you could use:

    Object.prototype.toSource 
        || (Object.prototype.toSource = function(){return JSON.stringify(this);})
    
    c = {a:100}
    //>Object
    c.toSource()
    //>"{"a":100}"
    

    cheers!

    0 讨论(0)
  • 2020-12-05 19:34

    I suggest using an existing library or plugin:

    • ExtJs: http://extjs.com/deploy/dev/docs/?class=Ext.util.JSON
    • jQuery: http://code.google.com/p/jquery-json/
    • Dojo: http://api.dojotoolkit.org/jsdoc/1.3/dojox.json.ref.toJson
    0 讨论(0)
  • 2020-12-05 19:38

    Try using a JSON serializer instead. toSource is Mozilla specific and not supported by IE.

    If you are just debugging then your best bet is going to be to install Firebug and use console.dir(emp); to print the contents of an object to the console window.

    Update: Just notice that on the link you posted it says, "Note: This method does not work in Internet Explorer!" And on the MDC page it says "Non-Standard".

    0 讨论(0)
提交回复
热议问题