Skip to content
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
Closed

CORS request fails when sending empty string for data #319

pamelafox opened this issue Nov 19, 2011 · 4 comments
Assignees
Milestone

Comments

@pamelafox
Copy link
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
Copy link
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
Copy link
Contributor Author

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
Copy link

ashok-v commented Mar 18, 2012

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

TIA

@pamelafox
Copy link
Contributor Author

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 as completed in 08e1b7c Apr 6, 2012
lopper pushed 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
Projects
None yet
Development

No branches or pull requests

3 participants