2014-01-26 31 views
0

我有一个Web服务来填充HTML标记。Web服务响应并将其分割为多个

[WebMethod] 
public string GetHelloMessage(string country) 
{ 
string test = "<div>amin</div>"; 
return test; 
} 

和jQuery是:

url: ServiceUrl, 
type: "POST", 
contentType: "application/json; charset=utf-8", 
dataType: "jsonp", 
timeout: 15000, 
success: function (d) { 
    if (d.length > 0) { 
     var c = JSON.parse(d); 
     if ($(".test1div").length) { 
      $(".test1div").html(c.Content1) 
     }if ($(".test2div").length) { 
      $(".test2div").html(c.Content2) 
     } 

如何编写Web服务,以填补test2div标签?如何添加以从Web服务返回文本?例如:

string Content1 = "<div>amin</div>"; 
string Content2 = "<div>reza</div>; 
string Content3 = Content1 + Content2; 
return Content3; 

请帮助我更多的解释。

+0

当你解决问题时,你应该接受一个答案和upvote。 – pid

回答

2

返回一个JSON对象不只是一个字符串:

[WebMethod] 
public string GetHelloMessage(string country) 
{ 
    return "{ name : 'amin', surname : 'reza' }"; 
} 

然后在页面上做到这一点:

var c = JSON.parse(d); 

$(".test1div").html(c.name); 
$(".test2div").html(c.surname); 

实际上,上面的代码是一个近似的答案。 您可以尝试反序列化两次:

JSON.parse((JSON.parse(d)) 

但真诚的,我怀疑这是一个很好的做法。如下所述,更好地利用内部机制:WebMethod return values in JSON format

我不知道返回值是什么,但您可以用alertconsole.log简单检查它。

+0

不适用于我!!!请解释更多。 –

+0

究竟是什么问题?显然,你必须调整代码的其他部分,比如div。 – pid

+0

当我不,添加(var c = JSON.parse(d);)它确实有效。 –