none
What is wrong with eval? RRS feed

  • Question

  • I have a question on EVAL function.

    I got following function using EVAL function in C# code in aspx file which was developed by previous web developer with Visual Studio 2010. Here is description of this function

    Purpuse:  Get column Data-A from table-A through sampleAjax and return the value.

       (1) Calling sampleAjax with parameter p1 and p2 at Line 8

       (2) The sampleAjax get column 'Data-A' which is 8 length varchar type value from 'table-A'

       (3) Set the return value from the sampleAjax to sReturn at Line 15

       (4) Return value at Line 22


    1 function SampleFunction(p1, p2) 2 { 3 4 var sReturn = ""; 5 $.ajax( 6 { 7 type: "POST", 8 url: "SampleASPX.aspx/SampleAjax", 9 data: "{'p1':'" + p1 + "','p2':'" + p2 + "'}", 10 contentType: "application/json; charset=utf-8", 11 async: false, 12 dataType: "json", 13 success: function(data) 14 { 15 sReturn = eval(data.d); 16 }, 17 error: function(response) 18 { 19 alert('sampleFunction failed: ' + response.status + ' ' + response.statusText); 20 } 21 }); 22 return sReturn; 23 }

    This code has been running fine for a long time. Recently we set 20 length value to column 'Data-A' in the 'Table-A'. That's the only changed. No changed in the function and ajax function above. When I run the program, the return value from the function is NULL without error, then I run it with debug mode, it goes fine until calling the EVAL at the line 15 as follows

    sReturn = eval(data.d);

    I can see the [data.d] contains correct value which is 20 length value which is return from the SampleAjax, but somehow the EVAL function set NULL to sReturn after that. I don't know why the EVAL function reject to set 20 length value. I am not sure what EVAL function for here. Can the function have something like configuration file that can set up if the length is more than 8, set null or something.

    Any advice will be gratefully appreciated. Thank you.. 



    • Edited by KELLY_IRVINE Wednesday, August 21, 2019 2:10 AM Correct mistypo
    • Moved by CoolDadTx Wednesday, August 21, 2019 1:45 PM ASP.NET related
    Wednesday, August 21, 2019 2:06 AM

Answers

  • Hi KELLY_IRVINE,

    Eval() in JS is used to calculate an expression or just a number which you can refer to 
    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval.
    If your data.d is not a pure number, this method returns null.

    If your data.d is a long pure number, this method will return this value in the form of scientific notation.

    Please share any example of your data.d, it will be very helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by KELLY_IRVINE Thursday, August 22, 2019 7:42 AM
    Wednesday, August 21, 2019 8:44 AM
  • Maybe you can use ‘sReturn = data.d’. It depends on the nature of these data. Show some details and examples about the value of data.d.


    • Edited by Viorel_MVP Wednesday, August 21, 2019 7:25 AM
    • Marked as answer by KELLY_IRVINE Thursday, August 22, 2019 7:42 AM
    Wednesday, August 21, 2019 7:23 AM

All replies

  • Maybe you can use ‘sReturn = data.d’. It depends on the nature of these data. Show some details and examples about the value of data.d.


    • Edited by Viorel_MVP Wednesday, August 21, 2019 7:25 AM
    • Marked as answer by KELLY_IRVINE Thursday, August 22, 2019 7:42 AM
    Wednesday, August 21, 2019 7:23 AM
  • Hi KELLY_IRVINE,

    Eval() in JS is used to calculate an expression or just a number which you can refer to 
    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval.
    If your data.d is not a pure number, this method returns null.

    If your data.d is a long pure number, this method will return this value in the form of scientific notation.

    Please share any example of your data.d, it will be very helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by KELLY_IRVINE Thursday, August 22, 2019 7:42 AM
    Wednesday, August 21, 2019 8:44 AM
  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 21, 2019 1:45 PM
  • Hi, Jack. Thank you for the prompt response. You are totally right! My test data was not pure number. It includes alphabets and numbers. I change the test data to only numbers, it works fine. I am very glad to find out the cause of the issue.

    Thank you very much for the link, too. Can I ask one more question? I am sorry it is may be because of my bad English, but I am still confused on what EVAL function is for.  Is it safer or better performance if calling EVAL when passing parameter over browser? If so, how can I pass parameter including number and alphabet to EVAL?

    Thursday, August 22, 2019 7:58 AM
  • Hi. Viorel! Thank you for your advice.

    You are right. It works fine after I take out EVAL() from the code. EVAL does not receive parameter including alphabet. Only numbers. Thank you very much for your advice!

    Can I ask one more question? What is actually EVAL function for? If it can be safer or faster performance to use EVAL, I guess I should keep using EVAL function and looking for the way to pass parameter including alpahbet to the EVAL function.. Your advice will be gratefully appreciated. Thank you..
    Thursday, August 22, 2019 8:03 AM