Vitalii
Vitalii

Reputation: 11071

Ajax request after success result of other reqjest (.NET + jQuery)

I need to get xml document from a server, then client signs it and sends back to server.

At server side I have web method which saves document:

[WebMethod]
public static void SaveSignedDocument(string SignedData)
{
    SignedCms signedCms = new SignedCms();
    ....
}

Then, I get document from a server and after success receive it I make client to sign it and send back. Here is Javascript

// get xml to sign
$.ajax({
        type: "POST",
        url: "Default.aspx/GetXMLReceipt",
        data: "{'ITN': " + ITN + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "xml",
        success: function (xml) {
            // xml file was got
            var xmlString = xmlToString(xmlData);
            // Sign data
            var SignedData = SignData(xmlString);

            // Send it to server
            $.ajax({
                url: 'Default.aspx/SaveSignedDocument',
                data: "{ 'SignedData': '" + SignedData + "' }",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data){
                    alert('Document was successfully sent!');
                }
                error: function (data, status, jqXHR) {
                    alert('Send signed data failed - ' + jqXHR);
                }
            });
        },
        error: function (data, status, jqXHR) {
            alert('Get data failed - ' + jqXHR);
        }
    });

The problem is that none of the alert at second requests ever fire. If I change request to synchronous everything is ok but why it does not work like written above? The server receives nothing and if we look to network traffic I see that request was interrupted. Why?

Upvotes: 0

Views: 436

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Encode your data properly, do not use any string concatenations. Here's the correct way:

data: JSON.stringify({ ITN: ITN }),

and on your second AJAX request:

data: JSON.stringify({ SignedData: SignedData })

The JSON.stringify method will ensure that you are sending valid JSON to your server by properly encoding the argument.

Upvotes: 1

Related Questions