snakepitbean
snakepitbean

Reputation: 227

Call jquery ajax "POST" with crossdomain jsonp from C# ASP.NET asmx

$.ajax({type: "post",
            url: "http://test.com/Register",
            data: {
            "UserID": '12',
            "FirstName": 'FN1'
            },

            accept: {
                json: "application/json",
                jsonp: "application/javascript"
            },
            dataType: "jsonp",
            crossDomain: true,
            beforeSend: function (xhr) {
                xhr.withCredentials = true;
            },
            xhrFields: {
                withCredentials: true
            },
            cache: false,
            success: function (data) {
                if(data.success)
        {
            alert("success");
        }
        else
        {
            alert("failed");
        }   
            }
});

I have this Ajax call working with javascript. But I'm trying to make this same call through C#, using HTTPWebRequest and am not getting the same results. When I do the call through C# it returns a webpage, instead of a json string,which is not right. And that is happening because the call for some reason is not proper. But the AJAX call return a proper json string back to me. I think it is because the ajax call specifies the datatype as "jsonp" and specifies it is cross domain.

The reason I need this to work in C# is because I want this C# call to be made inside a web service. So a form calls my web service(MyWebservice.asmx), which inturn makes this call in question to http://test.com/Register. So I cant really have AJAX / Jquery written into my webservice, and would rather have to use C# to make this call.

Any help?

    string data = "[SOME JSON DATA]";
    byte[] bytesArray = encoding.GetBytes(data);
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://test.com/Register");


            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.ContentLength = bytesArray.Length;
            using (Stream stream = httpRequest.GetRequestStream())
            {
                stream.Write(bytesArray, 0, bytesArray.Length);
                stream.Close();
            }

            HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            // Read the response content.
            responseFromServer = reader.ReadToEnd(); //This gives an HTML page as response

Upvotes: 1

Views: 2777

Answers (2)

snakepitbean
snakepitbean

Reputation: 227

Kevin B's reply helped. Like he mentioned JsonP posts are no different, and is plain old HttpWebRequest call. In this case it was a 'GET' for me.
Here the response I was expecting was pure json, and the service I was calling was returning JsonP (json with padding), hence end point on my side was throwing up.
Took care of the padding in JsonP and it was all good to go.
Hope this helps someone.

Upvotes: 1

Saranya
Saranya

Reputation: 2008

Can you try setting content type to @"application/json; charset=utf-8"? I would also suggest using JavaScriptSerializer to construct the JSON string.

Upvotes: 0

Related Questions