在JS中使用C#POST获取数据
本文关键字:C#POST 获取 数据 JS | 更新日期: 2023-09-27 18:27:29
我需要获得以下链接的函数resultado2()的结果字符串:
http://barion.inapi.cl/BuscaBiblio/proceso.js
作为第一步,我编写了以下代码:
string url = "http://barion.inapi.cl/BuscaBiblio/inicio.php";
Uri uri = new Uri(url);
HttpWebRequest request = HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
string buscar = "solicitud=" + 750 + "®istro=&descripcion=&solicitante=&d=1&m=1&y=" + 2003 + "&d2=31&m2=12&y2=" + 2003; // +"&nocache=0.1821407969109714";
byte[] data = Encoding.UTF8.GetBytes(buscar);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
WebResponse response = request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
在这里之前,一切都很好responseString'给了我'ok#1',这表示到目前为止只有一个结果。
然而,函数resultado2()返回的字符串会返回HTML代码,其中包括所需的表。我试过这个代码:
url = "http://barion.inapi.cl/BuscaBiblio/listado.php";
uri = new Uri(url);
request = HttpWebRequest.Create(url) as System.Net.HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
postData = "st=0&total=1";
data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
response = request.GetResponse();
responseString = new StreamReade
但它给了我500的错误。然而,在尝试Chrome的调试模式时,我显然可以检查"r"的值。
function resultado2() {
if(peticion_http.readyState == READY_STATE_COMPLETE) {
if(peticion_http.status == 200) {
mm = document.getElementById("resultado");
var r = peticion_http.responseText;
mm.innerHTML = r;
}
}
}
有什么想法吗?。提前谢谢。
为了澄清,您是否正在尝试将数据从客户端提交到服务器端?如果是这样的话,你的做法就有些奇怪了。您可以简单地执行以下操作:
function Calculate() {
var order = new Array();
var convertedOrder;
// Do whatever you need or want to validate.
// BuildOrder a function to build our object cleanly.
order.push(BuildOrder(product, quantity, price, shipping, other, location, tax));
// Convert object to Json
convertedOrder = JSON.stringify(order);
// The array and converting data into JSON would be wrapped in a loop.
$.ajax({
url: '<%= Page.ResolveUrl("~/Services/Order.aspx") %>',
data: { Order: convertedOrder },
type: 'POST',
success: function (order) {
var result = JSON.parse(order);
console.log(result[0].Total); // Returned data
}
}
上面的内容相当原始,但你至少明白了。然后在C#的服务器上,您执行一个object
,它将存储您传递的所有数据:
// Data to be read and parsed.
public class Returnable
{
public int Product { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Shipping { get; set; }
public decimal Other { get; set; }
public string Location { get; set; }
public decimal Tax { get; set; }
}
// Data to Send Back:
public class SendInformation
{
public string Tax { get; set; }
}
然后在.aspx
的代码隐藏中,您在Page_Load
:中执行此操作
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Returnable> deserialize = serializer.Deserialize<List<Returnable>>(order);
foreach(Returnable returnable in deserialize)
{
// Iterate through all of your passed data.
}
List<SendInformation> responseObject = new List<SendInformation>();
responseObject.Add(new SendInformation() { Total = "value", Tax = "value" });
string response = serializer.Serialize(responseObject);
Response.Write(response);
Response.End();
这将把数据从客户端推送到服务器,然后将服务器推回到客户端,以便您可以使用数据。甚至在新的功能和其他领域中称之为。
希望这能有所帮助。