schenker
schenker

Reputation: 953

jQuery Ajax fails only in Safari but other browsers are working

i have a javascript function to call ajax to write some stuff to database. So far it has worked on chrome and firefox without any problem. However, Safari randomly fails without much clue. (This is tested within localhost)

This is the error :

write failed : {"readyState":0,"status":0,"statusText":"error"}

This is my ajax function :

function writeToDB() {
      var data = {
        setting: 'some data'
      } ; 

    $.ajax({
        type: "POST",
        url: 'Admin/write_to_db',                    
        data: {
            data: 
       btoa(unescape(encodeURIComponent(JSON.stringify(data))))
        },                    
        success: function(response) {
            consoe.log('success');    
            console.log('response : ' + JSON.stringify(response));                              
        },
        error: function(response) {

            console.log('write failed : ' + 
        JSON.stringify(response));

        },
        complete: function(response) {
            console.log('completed');
        },        
    });

   }

I simplified the actual function for ease of reading. I have tried many similar responses given for a similar problem but nothing worked. Pls help me with this. Thank You.

Upvotes: 1

Views: 643

Answers (1)

schenker
schenker

Reputation: 953

Managed to fix the problem. The above function writeToDB is triggered by a button click. So all that i had to do is add event.preventDefault() to the function. That worked!

Upvotes: 1

Related Questions