0

我的观点 。

class EventRVSPResource(ModelResource):
event      = fields.ForeignKey(EventResource, 'event')
created_by = fields.ForeignKey(UserResource, 'created_by')  
class Meta:
    queryset = EventRsvp.objects.all()
    authorization = Authorization()
    resource_name = 'eventrvsp'
    filtering = {
        'event':ALL_WITH_RELATIONS,
        'created_by': ALL,

    }

但是使用 curl 发送数据工作正常。它显示 201

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"created_by": "/mobapp/api/v1/user/1/","event": "/mobapp/api/v1/eventlist/5/","notes": "Going","past_status": "DG","status": "Y"}' http://192.168.1.8:9000/mobapp/api/v1/eventrvsp/

我对 django-tastypie 的 Http 请求。但它仍然显示错误。我不知道它有什么问题

var url = "http://192.168.1.8:9000/mobapp/api/v1/eventrvsp/";
 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: " + this.responseText);
         alert('success');
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug();
         alert(e.error);
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 client.open("POST", url);
 // Send the request.
 client.send({"created_by": "/mobapp/api/v1/user/1/","event": "/mobapp/api/v1/eventlist/4/","notes": "Going","past_status": "DG","status": "Y"}); 
4

1 回答 1

0

问题是您需要将标头“content-type”设置为“application/json”并使用 JSON.stringify

你可以在这里找到参考:http: //django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post

注意:总是放在beforesetRequestHeader之后opensend

 var data = JSON.stringify({created_by:"/mobapp/api/v1/user/1/",event:"/mobapp/api/v1/eventlist/5/",notes:"Going",past_status:"DG",status:"Y"});
.
.
.
.
.




    client.open("POST", url);
client.setRequestHeader("Content-Type", "application/json");  
client.send(data); 
于 2013-02-27T17:38:48.257 回答