Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
制作一个发布当前URL的javascript书签_Javascript_Html_Post_Bookmarklet - Fatal编程技术网

制作一个发布当前URL的javascript书签

制作一个发布当前URL的javascript书签,javascript,html,post,bookmarklet,Javascript,Html,Post,Bookmarklet,我正在尝试制作一个javascript书签,将当前URL发布到我的API。例如,类似于Instapaper bookmarklet的功能。我编写了以下脚本,将其通过一个缩微器,并将其作为href添加到 我的Rails API的传入参数如下所示 <ActionController::Parameters {"url"=>"http://localhost:3000/!function()%7Bvar%20e=%22http://localhost:3001/api/v1/links/

我正在尝试制作一个javascript书签,将当前URL发布到我的API。例如,类似于Instapaper bookmarklet的功能。我编写了以下脚本,将其通过一个缩微器,并将其作为
href
添加到

我的Rails API的传入参数如下所示

<ActionController::Parameters {"url"=>"http://localhost:3000/!function()%7Bvar%20e=%22http://localhost:3001/api/v1/links/bookmarklet?url=%22 document.location.href %22", "id"=>"1", "xhr"=>"new%20XMLHttpRequest,xhr.open(%22POST%22,encodeURI(e)),xhr.send()}()", "format"=>:json, "controller"=>"api/v1/links", "action"=>"create_link_from_web"} permitted: false>
”http://localhost:3000/!函数()%7Bvar%20e=%22http://localhost:3001/api/v1/links/bookmarklet?url=%22 document.location.href%22,“id”=>“1”,“xhr”=>“新建%20XMLHttpRequest,xhr.open(%22POST%22,encodeURI(e)),xhr.send()}(),“format”=>:json,“controller”=>“api/v1/links”,“action”=>“从web创建链接”}允许:false>

因此,它确实在轨道上发送到了正确的路线,但这一切都搞砸了。非常感谢您的建议

您的
href
属性被视为字符串中没有明确协议的相对路径,例如
http:
javascript:
,等等。这就是为什么它将源域前置到您的URL字符串中

请记住,bookmarklet并不适用于所有浏览器,因此请确保您至少与您的目标受众兼容

若要更正此问题,请将HTML更改为

<a href='javascript:!function(){var e="http://localhost:3001/api/v1/links/bookmarklet?url="+document.location.href+"&id=1";xhr=new XMLHttpRequest,xhr.open("POST",encodeURI(e)),xhr.send()}();' id="button">post link</a>


您的HTML需要使用
进行修改,我甚至没有注意到minifier删除了
javascript:
协议。。。非常感谢。
<ActionController::Parameters {"url"=>"http://localhost:3000/!function()%7Bvar%20e=%22http://localhost:3001/api/v1/links/bookmarklet?url=%22 document.location.href %22", "id"=>"1", "xhr"=>"new%20XMLHttpRequest,xhr.open(%22POST%22,encodeURI(e)),xhr.send()}()", "format"=>:json, "controller"=>"api/v1/links", "action"=>"create_link_from_web"} permitted: false>
<a href='javascript:!function(){var e="http://localhost:3001/api/v1/links/bookmarklet?url="+document.location.href+"&id=1";xhr=new XMLHttpRequest,xhr.open("POST",encodeURI(e)),xhr.send()}();' id="button">post link</a>