javascript - 将 jquery 调用发送到外部 url

标签 javascript php jquery angularjs

这是我在 Angular js 中对外部 api 的调用

$http.post('http://api.quickblox.com/users.json', {
    token: quickbloxapitoken,
    user: {
        email: email,
        login: firstname,
        password: password
    }
        }, {
            'Content-Type': 'application/x-www-form-urlencoded'
        })
        .then(function(results) {
            var apiid = results.data.user.id;
     }

这里我的数据是像这样在两个json数组中发送的

enter image description here

当我尝试在 jquery 中做同样的事情时,我的电话是这样的

$.ajax({
    url: 'http://api.quickblox.com/users.json',
    type : 'POST',  
     data: { token: quickbloxapitoken, login: fbname, password: 'fbuserfbuser', email: fbmail},
    success: function(message)
    {
    console.log(message);
    }
    })

数据是这样发送的

enter image description here

如何使我的 jquery 数据像 angularjs 一样发送?

我的意思是这样?

enter image description here

最佳答案

您需要将请求的contentType指定为application/json

此外,如果您希望返回 JSON,最好也包含 dataType(尽管它可能会被自动猜到):

$.ajax({
    url: 'http://api.quickblox.com/users.json',
    type : 'POST',  
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({
       token: quickbloxapitoken,
       user: {
         login: fbname,
         password: 'fbuserfbuser',
         email: fbmail
       }
    })
});

参见 Documentation

关于javascript - 将 jquery 调用发送到外部 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240191/

相关文章:

Javascript 复杂图

javascript - 需要使用字符串滚动到具有相同 ID 的 div,而不使用 anchor 标记

javascript - 将新对象传递给 Angular 绑定(bind)时出现无限摘要错误

javascript - Vue 中的多个动态 HTML 参数

javascript - 如何检查 JASMINE (JS) 框架中点击时显示特定 div

php - 如何使用 YouTube Data API v3 检查 YouTube 视频是否存在

javascript - 从mysql和angular中删除数据

javascript - 如何防止滚动绑定(bind)到同一容器的其他元素?

php - 当列包含重复值时如何使用索引优化 mySQL 查询

javascript - 无论如何,要使用 jQuery 在移动设备上流畅地制作动画吗?