New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS request fails when sending empty string for data #319

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

Comments

Projects
None yet
3 participants
@pamelafox
Contributor

pamelafox commented Nov 19, 2011

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

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Nov 19, 2011

Collaborator

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 👍 👍

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

This comment has been minimized.

Show comment
Hide comment
@pamelafox

pamelafox Nov 19, 2011

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.

Contributor

pamelafox commented Nov 19, 2011

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

This comment has been minimized.

Show comment
Hide comment
@ashok-v

ashok-v Mar 18, 2012

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

TIA

ashok-v commented Mar 18, 2012

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

TIA

@pamelafox

This comment has been minimized.

Show comment
Hide comment
@pamelafox

pamelafox Mar 18, 2012

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);
Contributor

pamelafox commented Mar 18, 2012

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

if (settings.data === '') settings.data = null;
xhr.send(settings.data);

@ghost ghost assigned mislav Apr 5, 2012

@mislav mislav closed this in 08e1b7c Apr 6, 2012

lopper added a commit to buddydvd/zepto that referenced this issue Apr 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment