在异步函数运行期间调用ajax函数 [英] ajax call to function during async function is running
问题描述
嘿!
我在酒店搜索引擎上工作,收集了Expedia API的结果
我显示前15个结果然后我启动异步功能将其余结果保存在数据库中。
异步函数RequestMoreHotels(ByVal city As String,ByVal country As String ,ByVal countryCode As String,ByVal RegionIn As String,ByVal CheckOutDate As String,ByVal NrOfRooms As String,ByVal Room1 As String,ByVal Room2 As String,ByVal Room3 As String)As Task(Of JsonResult)
ReqCountry = Server.HtmlEncode(country)
ReqCity = Server.HtmlEncode(city)
ReqcountryCode = Server.HtmlEncode(countryCode)
ReqRegID = Server.HtmlEncode(RegionID)
ReqArrival = Server.HtmlEncode(checkInDate)
ReqDeparture = Server.HtmlEncode(CheckOutDate)
ReqRoomsRequested = Server.HtmlE ncode(NrOfRooms)
ReqRoom1 = Server.HtmlEncode(Room1)
ReqRoom2 = Server.HtmlEncode(Room2)
ReqRoom3 = Server.HtmlEncode( Room3)
RebelCurrency =会话(货币)
如果会话(文化)=de那么
LangSetting =de_DE
Else
LangSetting =en_US
结束如果
UserSession = Session(UserSessionID)
Await GetAsyncHotelsAsync()
返回Json(完成,JsonRequestBehavior.AllowGet)
结束函数
异步函数GetAsyncHotelsAsync()作为任务
获得更多结果并将其保存在数据库中
结束函数
所以,这个工作正常。
但是在此期间(当上述功能正在运行时)用户请求更多结果
(ajax请求:
公共函数GetNextResults()作为JsonResult
Dim GetHotels =(来自gh In _db2.temp_hotels其中gh.Session = UserSession和gh.shown = False命令由gh.ID Descending选择gh) .Take(20).ToList
Dim HotelHtml As New StringBuilder
每个项目在GetHotels
HotelHtml .Append(制作HTML)
下一页
返回Json(新的{.element = HotelHtml.ToString,.moreresults = MoreResults},JsonRequestBehavior.AllowGet)
结束函数
这个请求待了很长时间,当它最终被执行时,UserSession什么都没有。
如果你们中的某个人可以帮助我那将是很棒的
谢谢你提前
Che ers
Hey!
I working on a hotel search engine with gathers the result from a API of Expedia
I am displaying the first 15 results and then I am starting a async function to save the rest of the results in a database.
Async Function RequestMoreHotels(ByVal city As String, ByVal country As String, ByVal countryCode As String, ByVal RegionID As String, ByVal checkInDate As String, ByVal CheckOutDate As String, ByVal NrOfRooms As String, ByVal Room1 As String, ByVal Room2 As String, ByVal Room3 As String) As Task(Of JsonResult)
ReqCountry = Server.HtmlEncode(country)
ReqCity = Server.HtmlEncode(city)
ReqcountryCode = Server.HtmlEncode(countryCode)
ReqRegID = Server.HtmlEncode(RegionID)
ReqArrival = Server.HtmlEncode(checkInDate)
ReqDeparture = Server.HtmlEncode(CheckOutDate)
ReqRoomsRequested = Server.HtmlEncode(NrOfRooms)
ReqRoom1 = Server.HtmlEncode(Room1)
ReqRoom2 = Server.HtmlEncode(Room2)
ReqRoom3 = Server.HtmlEncode(Room3)
RebelCurrency = Session("Currency")
If Session("Culture") = "de" Then
LangSetting = "de_DE"
Else
LangSetting = "en_US"
End If
UserSession = Session("UserSessionID")
Await GetAsyncHotelsAsync()
Return Json("done", JsonRequestBehavior.AllowGet)
End Function
Async Function GetAsyncHotelsAsync() As Task
get more results and save them in a database
End Function
So, this is working fine.
But when in the meantime (while the above function is running) the user requesting more results
(ajax request:
Public Function GetNextResults() As JsonResult
Dim GetHotels = (From gh In _db2.temp_hotels Where gh.Session = UserSession And gh.shown = False Order By gh.ID Descending Select gh).Take(20).ToList
Dim HotelHtml As New StringBuilder
For Each item In GetHotels
HotelHtml.Append("make the HTML")
Next
Return Json(New With {.element = HotelHtml.ToString, .moreresults = MoreResults}, JsonRequestBehavior.AllowGet)
End Function
and this request is pending a long time and when it is finally executed, the UserSession is nothing.
It would be awesome if someone of you can help me with that
Thanks in advance
Cheers
推荐答案
我认为我正在做异步等待功能错误:
无论如何,我把代码更改为
公共函数RequestMoreHotels(ByVal city As String,ByVal country As String,ByVal RegionID As String,ByVal checkInDate As String,ByVal CheckOutDate As String,ByVal NrOfRooms As String ,ByVal Room1 As String,ByVal Room2 As String,ByVal Room3 As String)As JsonResult
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf GetHotelsAsync)
返回Json("完成",JsonRequestBehavior.AllowGet)
函数GetHotelsAsync(状态为对象)
做点什么
结束函数
所以在jQuery中我正在向RequestMoreHotels发出一个ajax请求,它返回immediatley完成但是在他做这个函数GetHotelsAsync开始之前
干杯
I think I am doing something wrong with the async await funcionality:
Anyway, I changed the code to
Public Function RequestMoreHotels(ByVal city As String, ByVal country As String, ByVal countryCode As String, ByVal RegionID As String, ByVal checkInDate As String, ByVal CheckOutDate As String, ByVal NrOfRooms As String, ByVal Room1 As String, ByVal Room2 As String, ByVal Room3 As String) As JsonResult
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf GetHotelsAsync)
Return Json("done", JsonRequestBehavior.AllowGet)
Function GetHotelsAsync(state As Object)
Do something
End Function
So in jQuery i am making an ajax request to RequestMoreHotels and it returns immediatley "done" BUT before he is doing that the function GetHotelsAsync gets started
Cheers
这篇关于在异步函数运行期间调用ajax函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!