2015-12-19 49 views
0

我通過Ajax這樣的電話獲得數據列表。ajax獲得使用asp.net後發佈數據ajax

$(document).ready(function() { 
    var hashtag = 'dilwale' 
    var accessToken = '16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82' 
    $.ajax({ 
     url: 'https://api.instagram.com/v1/tags/' + hashtag + '/media/recent?count=33&access_token='+ accessToken +'', 
     dataType: 'jsonp', 
     type: 'GET', 
     success: function (data) { 
     //Here I want to post data in database through ajax call Instead of showing 
      for (x in data.data) { 
       if (data.data[x].type == 'video') { 
        $('.instagram').append('<div style="border:1px solid orange"><video controls><source src="' + data.data[x].videos.standard_resolution.url + '" type="video/mp4"></video><span style="border:1px solid orange; dislay:block">Test1</span></div>'); 
       } else if (data.data[x].type == 'image') { 
        $('.instagram').append('<div style="border:1px solid orange"><img src="' + data.data[x].images.standard_resolution.url + '" ><span style="border:1px solid orange; display:block">"' + data.data[x].caption.text + '"</span><span style="border:1px solid orange; dislay:block">Test1</span></div>'); 
       } 
      } 
     }, 
     error: function (data) { 
     } 
    }) 
}); 

將數據插入功能如下

function InsertData() { 
    $.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: "Default.aspx/SaveData", 
     data: {}, 
     dataType: "json", 
     success: function (data) { 
      // Show data after success 
     }, 
     error: function() { 
      alert("Error while Showing update data"); 
     } 
    }); 
} 

在這裏,我要保存數據的列表。 我WEBMETHOD保存數據如下

[WebMethod]  
public static void SaveData(Document objDocs) //I have taken a class Document 
{  
    //Saving data in database 
} 

我不能讓它正確地保存數據獲得後,然後在頁面上顯示。

回答

0

您可以使用下面的代碼來從AJAX傳遞文檔數據:

//C# start 
//Sample Document class 
public class Document 
{ 
    public int DocumentId { get; set; } 
    public string Definition { get; set; } 
} 

//Javascript start 
//Use JSON.stringify to make a valid json string. 
var data = JSON.stringify(
    { 
     objDocs: { //objDocs should be same in SaveData method Document parameter 
      DocumentId: 1, 
      Definition: 'Test' 
     } 
    } 
); 

//ajax call to pass Document data 
$.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: 'Default.aspx/SaveData', 
    data: data, 
    dataType: "json", 
    success: function (data) { 
     alert(data); 
    } 
});