9 Replies - 1120 Views - Last Post: 02 September 2014 - 11:41 PM

#1 stuckman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 12

Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:05 AM

I'm trying to send data to my server as a `json` datatype via ajax. Currently in Firefox the server receives no data but in Chrome and IE the data is received and output on the server console.

This is my code:

 $(function () { 
    $(document).on('click', '.send', function(e) {
        var dataToSend = [];
        $('.form-group1').each(function () {
            var data = {};
            $(this).find(':input[data-name]').each(function () {
                data[$(this).data('name')] = $(this).val();
            });
            dataToSend.push(data);
        });

        $.ajax({
            url: '/pers1',
            type: 'POST',
            contentType:'application/json',
            data: JSON.stringify(dataToSend),
            dataType:'json'
          });        
           console.log(JSON.stringify(dataToSend))
      });
    });   


Why would Chrome and IE send this to the server but Firefox wouldn't? Is there a problem with the code or FF?

Is This A Good Question/Topic? 0
  • +

Replies To: Firefox not sending data via ajax to server

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:08 AM

Quote

Why would Chrome and IE send this to the server but Firefox wouldn't?

ask the error console.

This post has been edited by Dormilich: 02 September 2014 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 stuckman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 12

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:11 AM

I've checked and no error in console
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:15 AM

you might try text/plain unless you need the webserver parsing the request depending on the content-type
Was This Post Helpful? 0
  • +
  • -

#5 stuckman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 12

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:18 AM

Found solution - Added:

e.preventDefault();
after line 2
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:22 AM

so the .send button is actually a submit button? (you could have made it a click button to begin with)
Was This Post Helpful? 0
  • +
  • -

#7 stuckman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 12

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:32 AM

Actually this prevents me from sending the other data in the form.

The .send button submits other form data and this JSON is sent on the button click to another handler on my server side. So now this only sends the Json not the rest of the form data.

Can I use something else instead of e.preventDefault()?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:38 AM

Quote

The .send button submits other form data and this JSON is sent on the button click to another handler on my server side. So now this only sends the Json not the rest of the form data.

is this intended?

if not you can send the json data first, and when you get a successful return submit the form via Java­Script (which would require to not use a submit button in the first place).

This post has been edited by Dormilich: 02 September 2014 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#9 stuckman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-November 12

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 08:43 AM

Yes that is the intention to send both the form data and the json data on click of the .send button. But now it only sends the json data not the form data with e.preventDefault().
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4221
  • View blog
  • Posts: 13,362
  • Joined: 08-June 10

Re: Firefox not sending data via ajax to server

Posted 02 September 2014 - 11:41 PM

View Poststuckman, on 02 September 2014 - 05:43 PM, said:

But now it only sends the json data not the form data with e.preventDefault().

thatís what preventDefault() is supposed to do Ö
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1