locked
Javascript code working on localhost but not on server. RRS feed

  • Question

  • User-1651858287 posted

    I have three files Default.aspx, JsCode.js and Save_picture.aspx.cs.  Its works on the local machine but not on server2012.

    JsCode.js.
    function UploadPic() {

    // generate the image data
    // var Pic = document.getElementById("myCanvas").toDataURL("image/png");
    var Pic = document.getElementById("cnv").toDataURL("image/png");

    Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "")

    // Sending the image data to Server
    $.ajax({
    type: 'POST',
    url: 'Save_Picture.aspx/UploadPic',
    data: '{ "imageData" : "' + Pic + '" }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
    alert("Employee, Signature Uploaded.");
    }
    });
    }

    Default.aspx

    <button onclick="javascript:UploadPic();return false;" style="height: 26px">Upload Employee Signature </button>


    Save_picture.aspx.cs


    [WebMethod()]

    public static void UploadPic(string imageData)
    {

    HttpContext context = HttpContext.Current;

    string Pic_Path = context.Server.MapPath(context.Session["EmployeePic"].ToString() + ".png");


    using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
    {
    using (BinaryWriter bw = new BinaryWriter(fs))
    {
    byte[] data = Convert.FromBase64String(imageData);
    bw.Write(data);
    bw.Close();
    }
    }
    }

    when I click on the button

    <button onclick="javascript:UploadPic();return false;" style="height: 26px">Upload Employee Signature </button>

    nothing happens.

    when I check the browser inspector I get this 


    HTML1300: Navigation occurred.
    officalApp.aspx (1,1)

    SCRIPT7016: SCRIPT7016: Use of XMLHttpRequest with the synchronous flag set to true is deprecated due to its impact on user-perceived site performance.

    CSS3121: The media query -ms-viewport has been deprecated.

    2 SCRIPT7016: SCRIPT7016: Use of XMLHttpRequest with the synchronous flag set to true is deprecated due to its impact on user-perceived site performance.

    any help will be greatly appreciated. 

    Friday, June 22, 2018 9:09 PM

All replies

  • User-474980206 posted
    The errors are unrelated to the script you included. Most like the ajax call is failing, and as you have no error handle, it just fails silently. You should add error handling, but you can just use the browsers network tools, and view the server response from ajax call.
    Saturday, June 23, 2018 4:39 PM
  • User36583972 posted


    Hi sweetSteal,

    SCRIPT7016: SCRIPT7016: Use of XMLHttpRequest with the synchronous flag set to true is deprecated due to its impact on user-perceived site performance.

    May be you can try the following thread's solution.

    Specify the asynchronous option to be false to get a synchronous Ajax request.

    How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
    https://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re

    Besides, please use the browsers's network tools, and view the server response from the ajax call.


    Best Regards,

    Yong Lu

    Monday, June 25, 2018 5:15 AM
  • User-474980206 posted

    support for synchronous ajax is being dropped. You should not write new code using it.

    Monday, June 25, 2018 3:08 PM
  • User753101303 posted

    Hi,

    It's good to try first to see what happens but those message are unrelated to a possible error. Do you see the  the "Employee, Signature Uploaded" message ? You could also use F12 network to look at the Ajax query. Coult it be that you have a server side exception ? (for example because the account Under which your app runs is not allowed to write a file at the location you are using but seeing first what happens rather than trying to guess uis really the way to do IMO).

    Monday, June 25, 2018 5:44 PM