Xamarin Android jQuery AJAX在发布模式下不起作用

时间:2019-03-07 07:14:35

标签: jquery ajax xamarin xamarin.android android-webview

我在Xamarin中有一个Android应用程序。我的WebView中有一个HTML页面,并且我使用AJAX进行请求。在调试模式下,它可以完美工作,但在发布模式下,它会产生错误。

$.ajax({
   type: "POST",
   url: 'http://****.com',
   data: data,
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function(data, status) {
      alert('success')
   },
   error: function(xmlRequest) {
      alert('error')
   }
});

这是我的网络视图代码:

 WebView app_view = null;
 WebSettings app_web_settings = null;


protected override void OnCreate(Bundle savedInstanceState)
{
   base.OnCreate(savedInstanceState);
   // Set our view from the "main" layout resource
   SetContentView(Resource.Layout.Main);

   app_view = FindViewById(Resource.Id.webViewApp) as WebView;
   app_web_settings = app_view.Settings;
   app_web_settings.JavaScriptEnabled = true;
   app_web_settings.AllowUniversalAccessFromFileURLs = true;
   app_web_settings.DomStorageEnabled = true;
   app_web_settings.DatabaseEnabled = true;
   app_web_settings.SetRenderPriority(WebSettings.RenderPriority.High);
   app_view.SetLayerType(LayerType.Hardware, null);
   my_web_client = new MyWebViewClient(this);
   web_client = new WebChromeClient();
   app_view.SetWebViewClient(my_web_client);
   app_view.SetWebChromeClient(web_client);

  string app_url = "file:///android_asset/app_pages/home.html";
  app_view.LoadUrl(app_url);
  app_view.AddJavascriptInterface(new Foo(this), "foo");
}

1 个答案:

答案 0 :(得分:0)

我更改了ajax请求,并使用jsonp方法。在释放模式下可以成功运行。

 $.ajax({
        crossDomain: true,
        contentType: "application/json; charset=utf-8",
        url: 'http://****.com',
        data: data,
        dataType: "jsonp",
        success: function(data, status) {
          alert('success')
        },
        error: function(xmlRequest) {
          alert('error')
        }
      });
相关问题