0

I created a Rest webservice in visual studio 2010 like this:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DocService
{
    [WebInvoke(ResponseFormat = WebMessageFormat.Json,UriTemplate = "", BodyStyle = WebMessageBodyStyle.Wrapped)]
    public data GetCollection(Stream streamdata)
    {
    }
}

User will send request with json data to this service:

var JSONObject = '{"pCode": "123456789","mCode": "001","tCode": "","dArr": [{ "dCode": 26 },{ "dCode": 27 }],"sId": "sk"}';
var jsonData = JSON.parse(JSONObject);
var request = $.ajax({
    url: "http://myIp/path/to/service",
    type: "POST",
    data: jsonData,
    dataType: "json",
    success: function (data) {
        alert(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

First problem: How can I access that json in my code? For now I do like this (But json array can't serialize correct):

StreamReader reader = new StreamReader(streamdata);
string res = reader.ReadToEnd();
reader.Close();
reader.Dispose();

string resDecode = HttpUtility.UrlDecode(res);
NameValueCollection nvc = HttpUtility.ParseQueryString(resDecode);

var json = new JavaScriptSerializer().Serialize(
    nvc.AllKeys.ToDictionary(k => k, k => nvc[k])
);

Output:

res = "pCode=123456789&mCode=001&tCode=&dArr%5B0%5D%5BdCode%5D=26&dArr%5B1%5D%5BdCode%5D=27&sId=sk"
resDecode= "pCode=123456789&mCode=001&tCode=&dArr[0][dCode]=26&dArr[1][dCode]=27&sId=sk"
nvc = {pCode=123456789&mCode=001&tCode=&dArr%5b0%5d%5bdCode%5d=26&dArr%5b1%5d%5bdCode%5d=27&sId=sk}
json = "{\"pCode\":\"123456789\",\"mCode\":\"001\",\"tCode\":\"\",\"dArr[0][dCode]\":\"26\",\"dArr[1][dCode]\":\"27\",\"sId\":\"sk\"}"

Second problem: In ajax, after request sent, it's always done with error and xhr.status and thrownError are 0 and null but with Fiddler I can see json output

Hossein Rashno
  • 2,362
  • 1
  • 20
  • 44
  • 1
    Paste that `xhr.status` and `thrownError` error here. – Sandeep Kushwah Sep 27 '15 at 08:17
  • The second problem sounds like a cross-domain thing. Use [jsnop](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) or [the Access-Control-Allow-Origin HTTP header](http://stackoverflow.com/questions/6516591/how-to-implement-access-control-allow-origin-header-in-asp-net) – Marco Scabbiolo Sep 27 '15 at 08:17
  • @SandeepKushwah There is no error. Json format is incorrect. And for second problem "xhr.status = 0" – Hossein Rashno Sep 27 '15 at 08:19
  • also, you are posting a javascript object, that will never work, you need to post the pure JSON – Lars Anundskås Sep 27 '15 at 09:40

1 Answers1

1

Answer to the first problem:

Best way to use json in c# is to create a class with properties with exact name and type of the json object and then deserialize that json like this:

RequestClass reqParam = new RequestClass(); //Class with properties same as json object

StreamReader reader = new StreamReader(streamdata);
string res = reader.ReadToEnd();
reader.Close();
reader.Dispose();

JavaScriptSerializer js = new JavaScriptSerializer();
reqParam = js.Deserialize<RequestClass>(res);

Answer to the second problem:

My final javascript is like this and work fine:

var JSONObject = '{"pCode": "123456789","mCode": "001","tCode": "","dArr": [{ "dCode": 26 },{ "dCode": 27 }],"sId": "sk"}';
//Delete parse line
var request = $.ajax({
    url: "http://myIp/path/to/service",
    type: "POST",
    contentType: "application/json", //Added
    data: JSONObject, // This line changed
    dataType: "json",
    success: function (data) {
        alert(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});
Hossein Rashno
  • 2,362
  • 1
  • 20
  • 44