weixin_33671935 2015-09-06 07:46 采纳率: 0%
浏览 36

带有“ POST”的JSON Api调用

I try the following test-code to register a user at my Wordpress system (JSON Api User plugin is installed):

<form id="register" name="register" method="post">
<input id="submitLogin" type="submit" value="Abschicken" onclick="register()">
</form>


<script type="text/javascript" charset="utf-8">


function register() {
 $.ajax({
url: 'http://XXX.de/api/user/register/?username=xxx@test.de&email=xxx@test.de&nonce=d60besdfee&display_name=xxx@test.de&user_pass=rtzrtzrtz0&apikey=gfhrtzh465&callback=?',
type: "POST",
dataType: 'json',
success: function(data){

 }
 });
}

</script>

What is wrong with the code?

  • 写回答

1条回答 默认 最新

  • lrony* 2015-09-06 07:50
    关注

    The problem is that the endpoint you're calling uses JSONP, not just JSON. JSONP is a data format (JSON) and transmission mechanism (script element). It's inherently only GET, not POST, because under the covers what it really is is adding a script element to the page with a src attribute, which makes the browser get the script.

    If the endpoint supports Cross-Origin Resource Sharing, you can use POST (you'll want to remove the callback=? part of the URL). If it doesn't, you cannot use POST to talk to that endpoint cross-origin because of the Same Origin Policy.

    Side note: I can't see any reason your call needs to use POST, as you're not sending any data (no data property in the options for the ajax call). You may simply want to remove the type: "POST", change dataType: "json" to dataType: "jsonp", and remove the &callback=? (because jQuery will handle that when you specify dataType: "jsonp").

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog