2011-10-13 80 views
2

我已经修改了示例Demo以尝试发送json对象而不是字符串。该网站将其视为价值[对象]字符串而不是Json文本。我需要改变什么。新手如何使用scriptsharp将Json信息发送到Web服务

namespace DemoScript { 
// [Imported] 
// [IgnoreNamespace] 
public sealed class Person 
{ 
    public string FirstName; 
    public string LastName; 
} 

[GlobalMethods] 
internal static class HelloPage { 

    static HelloPage() { 
     // Add script that runs on startup as the script is loaded into 
     // the page 

     Element helloButton = Document.GetElementById("helloButton"); 

     Person p = new Person(); 

     helloButton.AddEventListener("click", delegate(ElementEvent e) { 
      InputElement nameTextBox = Document.GetElementById("nameTextBox").As<InputElement>(); 

      p.FirstName = nameTextBox.Value; 
      p.LastName = "Surname"; 

      XmlHttpRequest xhr = new XmlHttpRequest(); 
//   xhr.Open(HttpVerb.Get, "/HelloService.ashx?name=" + nameTextBox.Value.EncodeUriComponent()); 
      xhr.Open(HttpVerb.Get, "/HelloService.ashx?name=" + p); 

      ... 
     } 
    } 
} 

如果我传递p.FisrtName,它将按预期工作。

回答

0

谢谢DuckMaestro我现在正在工作。

以防万一,这是其他一些初学者的代码改变是有用的:

p.FirstName = nameTextBox.Value; 
p.LastName = "Surname"; 
XmlHttpRequest xhr = new XmlHttpRequest(); 
string text = Json.Stringify(p); 
xhr.Open(HttpVerb.Get, "/HelloService.ashx?name=" + text); 
+1

你或许应该编码查询字符串参数。使该text.EncodeUriComponent() –