weixin_33739646 2017-04-28 09:06 采纳率: 0%
浏览 13

Ajax发布中的怪异行为

I have this piece of code which works perfectly fine if I set a break point at code behind and let the value become zero and continue to run the rest of the code.

However it will break if i did not set a break point and let it run and the value will become 2 and break.

Does anyone have any idea why? The AJAX code below triggers when the user enters something in the textbox.

function getmachineinfo(serial) {
    $.ajax({
        url: '@Url.Action("actionname", "controller")',
        type: "POST",
        data: { "serial": serial },
        success: function (data) {alert('success')}, error: function (err) {
            alert(err)
        }}
public JsonResult getmachine(string serial)
{

    var machine= service.GetWarrantyDetailsBySerial(serial);     
    result.AccessoriesWs = new List<AccessoriesW>();

    //set break point here and let the result.AccessoriesW to be re declared and set to 0 and it will works.if i don set break point.result.AccessoriesW  will not have enough time to reset to zero.

    return Json(machine, JsonRequestBehavior.AllowGet);
}

Javascript error

A circular reference was detected while serializing an object of type

Result is parent object and AccessoriesW is child object resetting the child object will not get any errors.

Update solution

instead of using

result.AccessoriesWs = new List<AccessoriesW>();

i change it to below and problem solved. i think re declaring constructor is too slow for ajax.

   result.JuraAccessoriesWs.Clear();
  • 写回答

2条回答 默认 最新

  • helloxielan 2017-04-28 10:36
    关注

    Result looks like an instance (not local) variable... It is probably being overridden by other calls in the same controller? Do you do other AJAX calls at the same time?

    It is noting to do with timing, you should either construct the result object locally so it cannot be changed by other code during the action.

    评论

报告相同问题?