Sahrepoint 2019投掷“此时无法启动异步操作”仅在ajax调用上出现错误消息 [英] Sahrepoint 2019 throwing "An asynchronous operation cannot be started at this time" error message only on ajax call

查看:68
本文介绍了Sahrepoint 2019投掷“此时无法启动异步操作”仅在ajax调用上出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"此时无法启动异步操作。异步操作只能在异步处理程序或模块中启动,或者在页面生命周期中的某些事件中启动。如果在执行页面时发生此异常,请确保
表明页面已标记为<%@ Page Async = \" true \" %取代。此异常还可能表示尝试调用\"async void \"方法,在ASP.NET请求处理中通常不受支持。相反,异步方法应该
返回一个Task,并且调用者应该等待它。"


上面的异常是在ajax异步调用中抛出sharepoint 2019。但是在asp中按钮单击它工作正常


相同的应用程序在sharepoint 2013和sharepoint 2016中同时处理两种情况


示例调用如下所示。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP;

[WebMethod]

  &NBSP; &NBSP; &NBSP; public static void Test(string str)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //创建装订对象

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;绑定绑定= new BasicHttpBinding();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //创建服务的endpointAddress

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; EndpointAddress endpointAddress = new

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; EndpointAddress(" http://server/Service1.svc?wsdl");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //创建服务客户

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client1 = new ServiceReference1.Service1Client(binding,endpointAddress);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //使用ServiceClient呼叫服务方法

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client1.GetDataCompleted + = client_GetDataCompleted;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; client1.GetDataAsync(2);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(例外情况)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //TextBox1.Text = Convert.ToString(ex.InnerException);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; public static void client_GetDataCompleted(object sender,ServiceReference1.GetDataCompletedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP; }


Ajax调用


$(文档).ready(function(){

$(&#; Button2")。点击(功能(){

   var text =" this is test"; $
   $ .ajax({

     type:" POST",

     async:true,

      contentType:" application / json; charset = utf-8",&
     url:" ApplicationPage1.aspx / Test",

     data:" {'str':'" + text +"'}",

     dataType:" json",&
     success:function(data){

        

    },

    错误:功能(响应){

       alert(response);

   &n bsp;}

  &NBSP; });

   });

});




我已将页面Async属性标记为true。


对SharePoint 2019不支持异步Ajax呼叫的原因感到困惑?




$




解决方案


<我测试了下面的代码片段,对SharePoint 2019中的应用程序页面中的一个简单方法进行异步ajax调用,它正在工作:

 public string Get() 
{
System.Threading.Thread.Sleep(5000);
返回"哦!在最后一个漫长的过程结束时";
}


< script type =" text / javascript" SRC =" HTTPS://code.jquery.com/jquery-3.3.1.min.js">< /脚本> 
< script type =" text / javascript">


(document).ready(function(){
//调用LongProcess()
LongProcess();
函数firstfunction(){
console.log('我应该在LongProcess函数之后执行');
}
函数LongProcess(){


.ajax({
url:'ApplicationPage1.aspx / Get',
type:'GET',
async:true,
dataType :'json',
成功:函数(data,textStatus,xhr){
console.log(data);
},
错误:function(xhr,textStatus,errorThrown ){
console.log('a'+ textStatus);
}
});
//之后调用第一个函数ajax()函数defination
firstfunction();
}
});
< / script>

参考:


使用AJAX的Web API:了解jQuery ajax方法中的同步和异步调用


谢谢


最好的问候



"An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=\"true\" %>. This exception may also indicate an attempt to call an \"async void\" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it."

The above exception is throwing in sharepoint 2019 in ajax async call .But in asp button click it is working perfectly

The same application is working both cases in sharepoint 2013 and sharepoint 2016

The Sample call is shown as below..

            

[WebMethod]
        public static void Test(string str)
        {
            try
            {
                //Create object of the Binding
                Binding binding = new BasicHttpBinding();
                //Create endpointAddress of the Service
                EndpointAddress endpointAddress = new
                EndpointAddress("http://server/Service1.svc?wsdl");
                //Create Client of the Service
                client1 = new ServiceReference1.Service1Client(binding, endpointAddress);
                //Call Service method using ServiceClient
                client1.GetDataCompleted += client_GetDataCompleted;
                client1.GetDataAsync(2);
            }
            catch (Exception ex)
            {

                //TextBox1.Text = Convert.ToString(ex.InnerException);
            }
        }
        public static void client_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
        {

        }

Ajax call

$(document).ready(function () {
$("#Button2").click(function () {
    var text = "this is test";
    $.ajax({
        type: "POST",
        async:true,
        contentType: "application/json; charset=utf-8",
        url: "ApplicationPage1.aspx/Test",
        data: "{'str':'" + text + "'}",
        dataType:"json",
        success: function (data) {
            
        },
        error: function (response) {
            alert(response);
        }
    });
   });
});

I already marked the page Async attribute as true.

Confused why SharePoint 2019 is not supporting Async Ajax call?





解决方案

Hi,

I tested the code snippet below to make a async ajax call to a simple method in Application page in SharePoint 2019, it is working:

     public string Get()
        {
            System.Threading.Thread.Sleep(5000);
            return "Oh ! at last long process finish";
        }

        <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">


(document).ready(function () { //Calling to LongProcess() LongProcess(); function firstfunction() { console.log('I should execute after LongProcess function'); } function LongProcess() {


.ajax({ url: 'ApplicationPage1.aspx/Get', type: 'GET', async: true, dataType: 'json', success: function (data, textStatus, xhr) { console.log(data); }, error: function (xhr, textStatus, errorThrown) { console.log('a' + textStatus); } }); //Call to first function after ajax() function defination firstfunction(); } }); </script>

Reference:

Web API With AJAX: Understand Synchronous and Asynchronous Call in jQuery ajax Method

Thanks

Best Regards


这篇关于Sahrepoint 2019投掷“此时无法启动异步操作”仅在ajax调用上出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆