1

I am attempting to set up a way of sending a json to a server which will save it to a file. I am trying to design it for a PhoneGap application.

Currently I have:

tabulated = [Some object]
$.ajax({
    url: "http://localhost:33330/write-file",
    type: "POST",
    crossDomain: true,
    data: "data=" + JSON.stringify(tabulated),
    dataType: "json",
    contentType: "application/json",
    success: function(response) {
        alert(response);
    },
    error: function(request, textStatus, errorThrown) {
        alert(request);
        alert(textStatus);
        alert(errorThrown);
    }
});

The server end is a django server. In urlpatterns, I included url(r'^write-file$', views.WriteToFileView.as_view()), and in the views.py, I have:

class WriteToFileView(View):                                                                                                         
    def get(self, request, *args, **kwargs):                                                                                         
        return HttpResponse()                                                                                                        

    def post(self, request, *args, **kwargs):                                                                                        
        repr(request)                                                                                                                
        return HttpResponse("Yay!") 

The response is always:

XMLHttpRequest cannot load http://localhost:33330/write-file. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:33331' is therefore not allowed access.

What am I doing wrong?

Edit 1

This was marked as a duplicate of this question, however I do not believe it is. That question is failing due to a Null Origin. I am not having this issue. The error on that page does not mention any thing about a pre-flight check, whereas mine does.
Furthermore, I am not accessing any third-party pages. I am trying to set up communication between two local host servers, on different ports. (note this in the error)

The answers on the page mention:

  • 1) "not passing jsonp type to $.get" ---> I am not using $.get, nor am I passing a jsonp
  • 2) "using a file:// URL" ---> I am also not doing this.

All the same, I read through all the answers, but none of them seem to apply to my question. So can I please have an explanation on why this is a duplicate?

Community
  • 1
  • 1
kirypto
  • 129
  • 2
  • 14
  • I realize this is likely a duplicate, but my lack of understanding is not allowing me to understand why. Could I ask for a explanation on how my question is the same as that one? I am not getting a null origin, and my code is using `$.ajax`, not `$.get`. Again, I am sure this is a lack of understanding, so any explanation would be appreciated. Thanks – kirypto Jun 07 '16 at 01:26

1 Answers1

0

You should disable CORS both sides ..

  • just look it up on the net , it's a very usual problem and almost all the techs have it nowadays. I would like to help but I really don't master the techs you're working on. But still , check for "CORS filters" or "CORS disabling" in your field of interest and you'll find the adequate tutorial I'm pretty confident of it. – Hichem Al Abbessi Jun 09 '16 at 05:15