[英]Error in $Ajax ASP.NET web method call [async]
I am working over async web method (asmx file) and I need to call this method throughout an ajax jquery method however I am facing a lot of issues because the method uses also Entity Framework to run some other things. 我正在研究异步Web方法(asmx文件),并且需要在整个ajax jquery方法中调用此方法,但是由于该方法还使用Entity Framework来运行其他内容,因此遇到了很多问题。
Here is JavaScript: 这是JavaScript:
function SubmitStripeForm() {
// event.preventDefault();
stripe.createToken(card).then(function (result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
// stripeTokenHandler(result.token);
console.log();
var myToken = result.token.id;
$.ajax({
type: "POST",
url: "http://localhost:54355/Account/API/Stripe.asmx/MyStripePayment",
crossDomain: true,
async: false,
data: '{Tok: "' + myToken + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d) {
console.log("Good");
}
else {
console.log("Bad");
}
},
failure: function (response) {
console.log("HORRIBLE");
}
});
}
});
Here is the web method in asp.net c#: 这是asp.net c#中的Web方法:
[WebMethod(EnableSession = true)]
public async void MyStripePayment(string Tok)
{
string MyToken = Tok;
using (var context = new CompanyEntities())
{
var collection = (from p in context.COMPANY where p.Id == Company_Id select p);
foreach (var item in collection)
{
// Validation of the object
BillingManagement.Michael Maik = new BillingManagement.Michael();
Maik.name = "Michael";
Maik.id = "114060502";
Maik.number = "83290910";
#region Send Information To Stripe
CancellationToken Token = new CancellationToken();
string Url = "http://localhost:5000/testing/give-your-data";
using (var client = new HttpClient())
using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
{
var json = JsonConvert.SerializeObject(Maik);
using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
{
request.Content = stringContent;
using (var response = await client
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Token)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
string resString = await response.Content.ReadAsStringAsync();
JObject jObject = JObject.Parse(resString);
string message = (string)jObject["message"];
}
}
}
#endregion
}
}
and this is the error displayed: 这是显示的错误:
POST http://localhost:54355/Account/API/Stripe.asmx/MyStripePayment 500 (Internal Server Error)
have you tried using Try/Catch
to get more information about the error? 您是否尝试过使用“
Try/Catch
来获取有关该错误的更多信息? obviously it is something internal, better used it like that and try again to see what error it is. 显然这是内部问题,最好像这样使用它,然后再试一次以查看它是什么错误。
try
{
// Code
}
catch (Exception ex)
{
console.writeLine(ex);
}
OR 要么
You put an intervention in the catch to be able to see in local variables the values of the "Ex" or save the error somewhere to read it later. 您对捕获进行了干预,以便能够在局部变量中查看``Ex''的值或将错误保存在某个地方以供日后阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.