POST方法适用于firefox不适用于chrome

时间:2014-02-24 15:47:52

标签: jquery django google-chrome tastypie

我在Tastypie(Django)做过网络服务。我想做POST - 在FF上工作,在Chrome上不起作用。相反,POST出现OPTIONS和代码200.当项目移动到PhoneGap时,POST不能正常工作

请提示。

我的jQuery代码:

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

我的api.py - 我的Django项目中的TastyPie

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'],
        }

1 个答案:

答案 0 :(得分:0)

这可能与CORS有关。尝试添加$.support.cors = true;并查看Is it safe to use $.support.cors = true; in jQuery?以获取更详细的答案。您还可以使用服务的相对路径(如果可能的话)。

相关问题