2016-02-20 33 views
0

我不明白爲什麼我的JavaScript AJAX調用不會打我的C#方法。我在後面的代碼中構建它。jquery ajax調用不會觸及c#webmethod。點擊頁面加載雖然。

ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", @"swal.withForm({ 
 
         title: 'Database Credentials', 
 
         formFields: [{ 
 
          id: 'server', 
 
          placeholder: 'Server Name' 
 
         }, { 
 
          id: 'username', 
 
          placeholder: 'User Name' 
 
         }, { 
 
          id: 'password', 
 
          placeholder: 'Password', 
 
          type: 'password' 
 
         }, { 
 
          id: 'databaseName', 
 
          placeholder: 'Database Name' 
 
         }], 
 
         html: true, 
 
         confirmButtonText: 'Update' 
 
        }, function(isConfirm) { 
 
         console.log(JSON.stringify(this.swalForm)); 
 
         $.ajax({ 
 
          type: 'GET', 
 
          url: 'WebMethods.aspx/CheckCreateDatabase', 
 
          contentType: 'application/json; charset = utf - 8', 
 
          dataType: 'html', 
 
          data: JSON.stringify(this.swalForm), 
 
          success: function(data) { 
 
           swal('Success ' + data); 
 
          }, 
 
          error: function(data, success, error) { 
 
           swal('Error ' + error); 
 
          } 
 
         }); 
 
        });", true);

控制檯輸出這一結果爲JSON字符串,根據我的投入。

{"server":"dfd","username":"df","password":"dfd","databaseName":"dfd"} 

我的代碼背後

public partial class WebMethods : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var a = "a";  
    } 

[WebMethod] 
    public static string CheckCreateDatabase(string server, string username, string password, string databaseName) 
    { 
     return "it worked"; 
    } 
} 

它會打在頁面加載事件我的破發點,但它不會在CheckCreateDatabase塊擊中它。

我試了一堆不同的帖子,仍然無法讓它達到該方法。 有沒有人看到任何錯誤或任何我可以看看。謝謝。

回答

0

我想通了。我從一個網站複製了contenttype,並且裏面有空格。這是搞砸了。