javascript
  • c#
  • html
  • asp.net
  • http
  • 2016-03-04 65 views 1 likes 
    1

    我想在C#中編寫一個HttpModule,它生成一個空的POST請求到某個URL。我使用了以下代碼,我從另一個帖子中獲得:HTTP POST在Chrome中自動提交

    HttpContext.Current.Response.Clear(); 
    
    StringBuilder sb = new StringBuilder(); 
    sb.Append("<html>"); 
    sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>"); 
    sb.AppendFormat("<form name='form' action='{0}' method='post'>", HttpContext.Current.Request.Url.ToString()); 
    sb.AppendFormat("<input value='submit' type='submit'/>"); 
    //sb.AppendFormat("<input type='hidden' name='id' value='{0}'>", 1); 
    // Other params go here 
    sb.Append("</form>"); 
    sb.Append("</body>"); 
    sb.Append("</html>"); 
    
    HttpContext.Current.Response.Write(sb.ToString()); 
    
    HttpContext.Current.Response.End(); 
    

    在IE中,這可以正常工作。但是,在Chrome中,我只能看到生成的標記,就像body onload沒有正確觸發一樣。我錯過了什麼?

    +0

    此外,我注意到,document.forms是網頁上的空白。這可能與爲什麼表單本身沒有提交有關。 – awh112

    回答

    0

    嘗試設置這個值

    HttpContext.Current.Response.setContentType("text/html; charset=UTF-8"); 
    

    按照這個答案Html body "onload" is not working in Chrome

    +0

    我不得不稍微修改一下,但這似乎有伎倆。這裏是我使用的: 'HttpContext.Current.Response.ContentType =「text/html; charset = UTF-8」;' 感謝您的幫助! – awh112

    相關問題