views:

159

answers:

2

I used following jQuery to insert a data via Data Service. Event though I got 201 status response and the data is successfully inserted into my database, the system still regard it as a error and give me "failed" alert?

what did i missing here.

thank you in advance

$.ajax({
            type: "POST",
            url: "http://localhost:49223/Form/WebDataService.svc/XMLForm(guid'1eaef3a0-d6df-45bf-a8f6-3e7292c0d77e')/XMLRecord/",
            data: JSON.stringify(record),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function() {
                alert("Success");
            },
            error: function(xhr) {
                alert("fail");
            }
        });

UPDATE: Debug Message from Fire Bug Preferences

POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

jquery....min.js (line 127) POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

201 Created 6.7s

POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/

201 Created

get readyState 4

get responseText "{ "d" : {\r\n"__metadata"...\')/XMLForm"\r\n}\r\n}\r\n} }"

get responseXML null

get status 201

get statusText "Created"

+1  A: 

Solution:

even though I still cant work out how I getting error from previous code, I got this alternative solution which works for me. (at least for now).

would love to hear more ideas

thank you all

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "http://localhost:49223/Form/WebDataService.svc/XMLForm(guid'1eaef3a0-d6df-45bf-a8f6-3e7292c0d77e')/XMLRecord/",
            data: JSON.stringify(record),
            complete: function(xhr) {
                if (xhr.readyState == 4) {
                    if (xhr.status == 201) {
                        alert("Created");
                    }
                } else {
                    alert("NoGood");
                }
            }
            //                
            //                success: function(data) {
            //                    alert("Success");
            //                },
            //                error: function(xhr) {
            //                    alert("fail" + xhr);
            //                }
        });
D.J
A: 

I had this happen to me earlier. My problem was not including a '.json' extension at the end of my query string, so I was getting XML back. jQuery choked on trying to parse the xml as json, causing the error handler to get called.

YourParadigm