【问题标题】:POST method works on firefox does not work on chromePOST 方法适用于 firefox 不适用于 chrome
【发布时间】:2023-03-24 05:48:01
【问题描述】:

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

【问题讨论】:

  • 您检查控制台是否有错误?
  • 可能和CORS有关?

标签: jquery django google-chrome tastypie


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 2013-03-02
    • 2018-09-08
    • 2014-11-17
    • 2015-06-09
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多