0

I did a web service in Tastypie (Django). I want to do POST - works on FF, does not work on Chrome. Instead POST appears OPTIONS and code 200. POST does not work well when the project moved to PhoneGap

Please for hint.

My jQuery code:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
        $(document).ready(function(){

            $("#pay").click(function () {                   
                var data = JSON.stringify({
                    "measure": $('#odczytLicznika').val(),
                });

                $.ajax({
                    type: 'POST',
                    url: 'http://127.0.0.1:8080/api/entry/',
                    contentType: 'application/json',
                    data: data,                    
                    dataType: 'json',
                    processData: false,
                    success: redirect()
                });

                $('#validate').click(function () {
                    if ($('#viewCounter').val() == '') {
                        $('#money').text("Please put here value");
                        return;
                    }
                });

                function redirect()
                {
                    window.location = "profile_page.html";
                }
            });
        });
    </script>

My api.py - TastyPie in my Django Project

class urlencodeSerializer(Serializer):
    formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']
    content_types = {
        'json': 'application/json',
        'jsonp': 'text/javascript',
        'xml': 'application/xml',
        'yaml': 'text/yaml',
        'html': 'text/html',
        'plist': 'application/x-plist',
        'urlencode': 'application/x-www-form-urlencoded',
        }
    def from_urlencode(self, data, options=None):
        """ handles basic formencoded url posts """
        qs = dict((k, v if len(v) > 1 else v[0])
            for k, v in urlparse.parse_qs(data).iteritems())
        return qs

    def to_urlencode(self,content):
        pass

class EntryResource(ModelResource):
    class Meta:
        queryset = Entry.objects.all()
        resource_name = 'entry'
        authorization = Authorization()
        filtering = {"title": ALL}
        serializer = urlencodeSerializer() # IMPORTANT
        #serializer = Serializer(formats=['json', 'jsonp', 'xml', 'yaml', 'html', 'plist'])

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'auth/user'
        excludes = ['email', 'password', 'is_superuser']
        authorization = Authorization()
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'pub_date': ['exact', 'lt', 'lte', 'gte', 'gt'],
        }
Mariusz
  • 177
  • 1
  • 4
  • 11

1 Answers1

0

Probably this has to do with CORS. Try to add $.support.cors = true; and see Is it safe to use $.support.cors = true; in jQuery? for a more detailed answer. You could also use a relative path to your service(if this is possible).

Community
  • 1
  • 1
GuyT
  • 4,316
  • 2
  • 16
  • 30