【问题标题】:sending jquery call to external url将 jquery 调用发送到外部 url
【发布时间】:2015-07-06 07:39:17
【问题描述】:

这是我在 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 数组发送的

当我尝试在 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);
    }
    })

数据是这样发送的

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

我的意思是这样?

【问题讨论】:

    标签: javascript php jquery angularjs


    【解决方案1】:

    您需要将请求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

    【讨论】:

    • 谢谢.. 我更改了contentType,但我的数据应该是第一个 json 中的 token 和第二个 json 中的 email,login,password。就像我给出的最终截图一样..
    • @user4989228 我相应地修改了答案
    • 谢谢,但我收到XMLHttpRequest cannot load http://api.quickblox.com/users.json' 错误:(你对此有任何想法吗?
    • @user4989228,查看我的更新。我错过了JSON.stringify 部分。
    • @user4989228,请不要暴露真实的身份验证凭据。此外,您似乎没有使用我修改后的答案,其中包括对 JSON.stringify 的调用。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2020-03-25
    • 2021-07-26
    • 1970-01-01
    • 2019-01-25
    • 2011-06-04
    相关资源
    最近更新 更多