Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ajax async:false同步请求 #43

Open
dhui68 opened this issue Oct 30, 2020 · 11 comments
Open

ajax async:false同步请求 #43

dhui68 opened this issue Oct 30, 2020 · 11 comments
Labels
enhancement New feature or request resolved resolved issue and for help people find tip tip tip for help coding

Comments

@dhui68
Copy link

dhui68 commented Oct 30, 2020

ajax async:false同步请求不生效,不会等success回调后再执行下面代码,

我尝试修复,使用window.prompt("SyncCall",obj),但是发现iOS14不行,iOS14以下正常了。

请问有什么好的方案解决同步问题吗?

@karosLi
Copy link
Owner

karosLi commented Nov 5, 2020

嗯,你用的是 pod 'KKJSBridge/AjaxHook' 是这个版本吗?
这个版本是统一都当做异步来处理的。

我觉得你还是通过 await 来解决吧。

@erickyim
Copy link

erickyim commented Nov 19, 2020

同求同步解决方案。调试了很久才发现不支持async:false。前端有很多地方使用了async:false,不能要求他们都改成await。🤦‍♀️

@karosLi
Copy link
Owner

karosLi commented Nov 20, 2020

同求同步解决方案。调试了很久才发现不支持async:false。前端有很多地方使用了async:false,不能要求他们都改成await。🤦‍♀️

你用的是哪种 ajax hook 方案?


# 分别提供了 ajax hook 和 ajax urlprotocol hook 两种方案,可以根据具体需求自由选择。
# 只能选择其中一个方案,默认是 ajax protocol hook。
pod 'KKJSBridge/AjaxProtocolHook'
pod 'KKJSBridge/AjaxHook'

@karosLi
Copy link
Owner

karosLi commented Nov 20, 2020

pod 'KKJSBridge/AjaxProtocolHook', '1.3.2'
pod 'KKJSBridge/AjaxHook', '1.3.2'

ajax hook 和 ajax urlprotocol hook 方案都支持如下特性:

2020.11.21 (1.3.2)

  • 支持 JSBridge 同步调用
  • 支持纯文本的 ajax 同步请求(还不支持 Blob 和 表单)
  • 支持通过 document.cookie 同步从 NSHTTPCookieStorage 读取最新的 Cookie

已经支持 ajax 同步调用了,目前只支持纯文本的同步调用,还不支持表单的同步调用,不过一般情况下也够用了。

@karosLi
Copy link
Owner

karosLi commented Nov 20, 2020

@wjiuxing

@wjiuxing
Copy link
Contributor

wjiuxing commented Nov 21, 2020 via email

@karosLi
Copy link
Owner

karosLi commented Nov 21, 2020

另外关于 associated,底层实际上还是一个 hash map,对象指针作为 key,时间复杂度是 O(1),性能是上还可以的,只是内存会增加一点。

void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
    // retain the new value (if any) outside the lock.
    ObjcAssociation old_association(0, nil);
    id new_value = value ? acquireValue(value, policy) : nil;
    {
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.associations());
        disguised_ptr_t disguised_object = DISGUISE(object);
        if (new_value) {
            // break any existing association.
            AssociationsHashMap::iterator i = associations.find(disguised_object);
            if (i != associations.end()) {
                // secondary table exists
                ObjectAssociationMap *refs = i->second;
                ObjectAssociationMap::iterator j = refs->find(key);
                if (j != refs->end()) {
                    old_association = j->second;
                    j->second = ObjcAssociation(policy, new_value);
                } else {
                    (*refs)[key] = ObjcAssociation(policy, new_value);
                }
            } else {
                // create the new association (first time).
                ObjectAssociationMap *refs = new ObjectAssociationMap;
                associations[disguised_object] = refs;
                (*refs)[key] = ObjcAssociation(policy, new_value);
                object->setHasAssociatedObjects();
            }
        } else {
            // setting the association to nil breaks the association.
            AssociationsHashMap::iterator i = associations.find(disguised_object);
            if (i !=  associations.end()) {
                ObjectAssociationMap *refs = i->second;
                ObjectAssociationMap::iterator j = refs->find(key);
                if (j != refs->end()) {
                    old_association = j->second;
                    refs->erase(j);
                }
            }
        }
    }
    // release the old value (outside of the lock).
    if (old_association.hasValue()) ReleaseValue()(old_association);
}

@wjiuxing
Copy link
Contributor

wjiuxing commented Nov 21, 2020 via email

@karosLi karosLi added the enhancement New feature or request label Nov 21, 2020
@erickyim
Copy link

同求同步解决方案。调试了很久才发现不支持async:false。前端有很多地方使用了async:false,不能要求他们都改成await。🤦‍♀️

你用的是哪种 ajax hook 方案?


# 分别提供了 ajax hook 和 ajax urlprotocol hook 两种方案,可以根据具体需求自由选择。
# 只能选择其中一个方案,默认是 ajax protocol hook。
pod 'KKJSBridge/AjaxProtocolHook'
pod 'KKJSBridge/AjaxHook'

用的 pod 'KKJSBridge/AjaxProtocolHook',
jQuery代码如下:

$.ajax({
    type: "POST",
    async: false,
    headers: {
        "Content-Type": 'application/x-www-form-urlencoded;charset=utf-8'
    },
    url: "/home/page",
    data: {
        from: "home"
    },
    success: function (data) {
        console.log(111);
        alert("success: \n" + data);
    },
    error: function (error) {
        console.log("error");
        alert("error: \n" + data);
    }
});

@karosLi
Copy link
Owner

karosLi commented Nov 23, 2020

pod 'KKJSBridge/AjaxProtocolHook', '1.3.4'

@erickyim 试下这个版本

@erickyim
Copy link

pod 'KKJSBridge/AjaxProtocolHook', '1.3.4'

@erickyim 试下这个版本

已经可以了,非常感谢~

@karosLi karosLi added tip tip for help coding resolved resolved issue and for help people find tip labels Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolved resolved issue and for help people find tip tip tip for help coding
Projects
None yet
Development

No branches or pull requests

4 participants