curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
Ở trên hoạt động tốt nhưng khi tôi cố gắng sao chép POST trong ajax dưới đây tôi nhận được lỗi 500.Ajax POST và Django Tastypie
$.ajax({
type: 'POST',
url: 'http://localhost:8000/geo/api/geolocation/',
data: '{"latlong": "test"}',
success: latlongSaved(),
dataType: "application/json",
processData: false,
});
Thông báo lỗi là:
{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." .... }
Cần lưu ý đây là miền chéo và tôi đang sử dụng django-crossdomainxhr-middleware.py tìm thấy qua git: ý chính
Nếu tôi thêm một kiểu nội dung cuộc gọi ajax như thế này:
contentType: "application/json"
tôi nhận được lỗi này lại:
XMLHttpRequest cannot load http://localhost:8000/geo/api/geolocation/. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Request URL:http://localhost:8000/geo/api/geolocation/
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Access-Control-Request-Headers:Origin, Content-Type, Accept
Access-Control-Request-Method:POST
Origin:http://localhost:3000
Response Headersview source
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin:*
Content-Type:text/html; charset=utf-8
Date:Tue, 23 Aug 2011 07:59:49 GMT
Server:WSGIServer/0.1 Python/2.6.1
Kiểm tra tài liệu - bạn có thể cần đặt tùy chọn json trong chuỗi url. – Marcin
Bạn có thể đăng sự khác biệt giữa các tiêu đề yêu cầu 'curl' và các tiêu đề jQuery không? Bạn có thể thấy các tiêu đề jQuery trong tab giao diện điều khiển bên trong Firebug. –