CORS request fails when sending empty string for data #319

Closed
pamelafox opened this Issue Nov 19, 2011 · 4 comments

Projects

None yet

3 participants

@pamelafox
Contributor

If I specify an empty string for data in an AJAX request, the request fails due to CORS error ("not allowed by Access-Control-Allow-Origin."). It works fine with jQuery $.ajax and works fine if I pass in null instead. Perhaps it's a mistake to pass in an empty string, I'm not sure, but it took some debugging to figure out why it wasn't working.

Code:

    $.ajax({
        url: url,
        type: 'POST',
        data: '',
        dataType: 'json',
        xhrFields: {
          withCredentials: true
        },
        success: function(responseJSON, textStatus, xhr) {
        },
        error: function(xhr, textStatus, errorThrown) {
        }
     });
@mislav
Collaborator
mislav commented Nov 19, 2011

Interesting. Maybe we should turn empty string into null internally to work around this.

Thanks for the report, and all the contributions you've made! Seriously awesome 👍 👍

@pamelafox
Contributor

Yeah I added a line that turns empty string into null in my zepto.js, and that works.

I should clarify that it fails in Chrome - this could well be a browser-specific issue. CORS issues often are.

@ashok-v
ashok-v commented Mar 18, 2012

@pamelafox can you share the fix that is working for you

TIA

@pamelafox
Contributor

I added the check for empty string right before the call to xhr.send:

if (settings.data === '') settings.data = null;
xhr.send(settings.data);
@mislav mislav was assigned Apr 5, 2012
@mislav mislav closed this in 08e1b7c Apr 6, 2012
@lopper lopper added a commit to buddydvd/zepto that referenced this issue Apr 24, 2013
@mislav @lopper mislav + lopper ajax: work around Chrome CORS bug when data is empty
Fixes #319
46e3f42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment