2011-10-13 36 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() –

相關問題