dForm from JSON returned from webmethod

99 views
Skip to first unread message

Mazolo

unread,
Sep 12, 2013, 3:38:39 AM9/12/13
to jquery...@googlegroups.com
Please help
I'm trying to render HTML returned from WebMethod in json format as below:

 

$(document).ready(

 

function () {

$.ajax({

type:

"POST",

contentType:

"application/json; charset=utf-8",

url:

"GetHTML.asmx/GetMarkup",

data:

"{}",

dataType:

"json",

success:

function (msg) {

var data = msg.hasOwnProperty("d") ? msg.d : msg;

//alert(data);

OnSucessCallBack(data);

},

error:

function (xhr, status, error) {

alert(xhr.statusText);

}

});

function OnSucessCallBack(data) {

//alert(data);

$(

"#myform").dform({

"action": "index.htm",

"method": "get",

"html": data

});

}

});

 

Data is as below:

[{ "name": "username", "id": "txt-username", "type": "text", "caption": "Username" }, { "name": "password", "id": "txt-password", "type": "password", "caption": "Password"}]

The data is rendered as text instead.

 

 

 

 

 

 

Daff

unread,
Sep 12, 2013, 9:34:50 AM9/12/13
to jquery...@googlegroups.com
This is most likely because your webserver doesn't return application/json as the content type resulting in data itself being a string (you can easily find that out by console.log(typeof data)).

Mazolo

unread,
Sep 12, 2013, 9:47:13 AM9/12/13
to jquery...@googlegroups.com
Thanks for response Daff
This is the webmethod, is there anything wrong with it? :
 

[

WebMethod]

[

ScriptMethod(ResponseFormat = ResponseFormat.Json)]

public string GetMarkup()//string formId

{

FormControl[] fcArr = new FormControl[2];

string markUp = "";

FormControl fc = new FormControl();

fc.name =

"username";

fc.id =

"txt-username";

fc.caption =

"Username";

fc.type =

"text";

fcArr[0] = fc;

fc =

new FormControl();

fc.name =

"password";

fc.id =

"txt-password";

fc.caption =

"Password";

fc.type =

"password";

fcArr[1] = fc;

markUp =

JsonConvert.SerializeObject(fcArr);

return markUp;

}

Daff

unread,
Sep 12, 2013, 10:59:04 AM9/12/13
to jquery...@googlegroups.com
Hm, I'm not too familiar with .NET but you probably have to somehow tell it to send application/json. A good place to ask usually is stackoverflow.com.

Mazolo

unread,
Sep 13, 2013, 3:04:57 AM9/13/13
to jquery...@googlegroups.com
Thanks Daff
You solved the problem, the webmethod returned a string that I had to parse to an array. I used $.parseJSON(data) to parse the string to an array. Thanks again you saved my life.
Reply all
Reply to author
Forward
0 new messages