如何读取支付结果MVC的页面响应
how to Read page respond of the payment result MVC
基本上我的应用程序类似于 link 但我的应用程序是在 MVC C# 上。
-提出付款请求。 -阅读回应。 - 将响应传递给我们的 api.
https://developer.2c2p.com/docs/prepare-payment-request
我的付款请求代码:
function CreateForm(parameters)
{
var form = $('<form id="PaymentForm1"></form>');
var path = url;
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function (key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
console.log(key, value);
form.append(field);
});
$(document.body).append(form);
form.submit();
}
所以我现在的问题是如何读取响应结果...并将其传递给我们的 api。
(与此类似,但在 MVC 上 https://developer.2c2p.com/docs/read-payment-response)
我尝试使用此代码.. WebRequest request = WebRequest.Create(resultURL);
但是会重新加载页面,数据会消失。
[HttpGet]
public ActionResult getResultValues()
{
WebRequest request = WebRequest.Create(resultURL);
request.Method = "GET";
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
return View(responseFromServer);
}
有更好的方法吗?我也是 MVC 的新手
@chenz101
查看我的代码
为以下格式制作 class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AspNetMvcJqueryAjaxSerializeForm.Models
{
public class FriendModel
{
public string FriendName { get; set; }
public string Phone { get; set; }
public string State { get; set; }
}
}
控制器
// POST: Friend/AddFriend
[HttpPost]
public ActionResult AddFriend(FriendModel fm)
{
//Write your database insert code / activities
return RedirectToAction("create");
}
这是您的 MVC 视图 html 页面代码
<form id="friendform">
<table>
<tr>
<td>Friend Name</td>
<td><input id="txtFriendName" name="FriendName" type="text" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input id="txtPhone" name="Phone" type="text" /></td>
</tr>
<tr>
<td>State</td>
<td><input id="txtState" name="State" type="text" /></td>
</tr>
</table>
<input id="btnsubmit" type="button" value="Submit"/>
</form>
这是您的ajax呼叫代码
<script>
$(document).ready(function () {
$("#btnsubmit").click(function (e) {
//Serialize the form datas.
var valdata = $("#friendform").serialize();
//to get alert popup
alert(valdata);
$.ajax({
url: "/Friend/AddFriend",
type: "POST",
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: valdata
});
});
});
</script>
基本上我的应用程序类似于 link 但我的应用程序是在 MVC C# 上。
-提出付款请求。 -阅读回应。 - 将响应传递给我们的 api.
https://developer.2c2p.com/docs/prepare-payment-request
我的付款请求代码:
function CreateForm(parameters)
{
var form = $('<form id="PaymentForm1"></form>');
var path = url;
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function (key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
console.log(key, value);
form.append(field);
});
$(document.body).append(form);
form.submit();
}
所以我现在的问题是如何读取响应结果...并将其传递给我们的 api。 (与此类似,但在 MVC 上 https://developer.2c2p.com/docs/read-payment-response)
我尝试使用此代码.. WebRequest request = WebRequest.Create(resultURL);
但是会重新加载页面,数据会消失。
[HttpGet]
public ActionResult getResultValues()
{
WebRequest request = WebRequest.Create(resultURL);
request.Method = "GET";
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
return View(responseFromServer);
}
有更好的方法吗?我也是 MVC 的新手
@chenz101
查看我的代码
为以下格式制作 class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AspNetMvcJqueryAjaxSerializeForm.Models
{
public class FriendModel
{
public string FriendName { get; set; }
public string Phone { get; set; }
public string State { get; set; }
}
}
控制器
// POST: Friend/AddFriend
[HttpPost]
public ActionResult AddFriend(FriendModel fm)
{
//Write your database insert code / activities
return RedirectToAction("create");
}
这是您的 MVC 视图 html 页面代码
<form id="friendform">
<table>
<tr>
<td>Friend Name</td>
<td><input id="txtFriendName" name="FriendName" type="text" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input id="txtPhone" name="Phone" type="text" /></td>
</tr>
<tr>
<td>State</td>
<td><input id="txtState" name="State" type="text" /></td>
</tr>
</table>
<input id="btnsubmit" type="button" value="Submit"/>
</form>
这是您的ajax呼叫代码
<script>
$(document).ready(function () {
$("#btnsubmit").click(function (e) {
//Serialize the form datas.
var valdata = $("#friendform").serialize();
//to get alert popup
alert(valdata);
$.ajax({
url: "/Friend/AddFriend",
type: "POST",
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: valdata
});
});
});
</script>