使用ajax请求后,在将数据填充到引导模式时解决错误

时间:2017-10-25 14:07:26

标签: jquery

单击按钮后,我使用bootstrap模式动态显示数据。该按钮从数据库收集数据,然后转到ajax请求,该请求将数据发送到带有模态的页面。数据发送良好,但模态显示错误:

  

parsererror:SyntaxError:JSON.parse:JSON数据第2行第1列的意外字符:

我该怎么办?

[Application]
public class MyApplication : Application
{
    public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
        RegisterActivityLifecycleCallbacks(new AppLifecycleListener());
    }
}

public class AppLifecycleListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks
{
    private int numStarted = 0;

    ...

    public void OnActivityStarted(Activity activity)
    {
        if (numStarted == 0)
        {
            // app went to foreground
            Toast.MakeText(Android.App.Application.Context, "App is in foreground", ToastLength.Short).Show();
        }
        numStarted++;
    }

    public void OnActivityStopped(Activity activity)
    {
        numStarted--;
        if (numStarted == 0)
        {
            // app went to background
            Toast.MakeText(Android.App.Application.Context, "App is Going to Background", ToastLength.Short).Show();
        }
    }
}

modalbody.php

<script>
    function detailsmodal(Productid) {
        var data = {"Productid": Productid};
        jQuery.ajax({
            url: '/project/includes/modalbody.php',
            data: data,
            dataType: 'json',
            type: 'POST',
            success: function (data) {
                jQuery('.modal-body').append(data);
                jQuery('#details-modal').modal('toggle');
            }
        }).fail(function (xhr, status, error) {
            alert('error:' + status + ':' + error + ':' + xhr.responseText)
        }).always(function () {
            location.reload();
        });
    }
 </script>

2 个答案:

答案 0 :(得分:0)

使用Json的Ajax

<script>
    function detailsmodal(Productid_value) {
    var str = {Productid: Productid_value};
    jQuery.ajax({
        type: "POST",
        url: "/project/includes/modalbody.php",
        data: str,
        dataType: "json",
        cache: false,
        success: function(response)
        {
            var respons = response;
            //var type = respons.status
            jQuery('.modal-body').append(respons.Productid);
            jQuery('#details-modal').modal('toggle');
        }
    }).fail(function (xhr, status, error) {
        alert('error:' + status + ':' + error + ':' + xhr.responseText)
    }).always(function () {
        location.reload();
    });
}
</script>

答案 1 :(得分:0)

似乎你返回的实际上是一个HTML而不是JSON改变你的Ajax

dataType: 'json',dataType: 'HTML',您也可以尝试使用dataType: 'text',

这应该像您的代码中看起来那样,.append()将包含您的HTML响应