weixin_33695082 2014-05-05 08:30 采纳率: 0%
浏览 44

Ajax接听电话不起作用

i have following kind of url

  http://11.11.11.11/url/index.php?data={method:method_name,params:{param1:value,param2:value,}}

and i want to pass data from backbone view to ajax class which has following kind of structure

define(['jquery', 'backbone'],
function($, Backbone) {
    var ajax = {
        getDataCall: function(url, data, successCallBack, failureCallBack) {
            var that = this;
            //console.log(method);
            //console.log(successCallBack);
            that.showLoading();
            $.ajax({
                url: url,
                method: 'get',
                data: data,
                // contentType:"application/json; charset=utf-8",
                success: function(data) {
                    that.hideLoading();
                    //alert("success ajax");
                    successCallBack(data);
                },
                fail: function(data) {
                    that.hideLoading();
                    failureCallBack(data);
                }
            });
        },
        showLoading: function() {
            $.blockUI();
            //$.blockUI({ message: '<img src="img/ajax-loader.gif" />' })
        },
        hideLoading: function() {
            $.unblockUI();
        }
    };
    return ajax;
});

and from backbone view i am calling

var dataToSend={"method":"method","params":{"param2":"param2"}}  

ajax.getDataCall("http://11.11.11.11/url/index.php?",dataToSend,successCallBack,failureCallback)

but it is not turning into correct url. What is wrong in this code?

  • 写回答

1条回答 默认 最新

  • weixin_33720186 2014-05-05 09:35
    关注

    This is an interesting setup. I never really seen JSON format in an URL before. BUT, basically jQuery takes a object literal in the data attribute and serializes it on the default contentType.

    Anyway, the solution is you need to 1) stringify the object, 2) take away the quotes, and 3) put "data=" as you have placed there:

    var dataToSend={"method":"method","params":{"param2":"param2"}};
    dataToSend = 'data=' + JSON.stringify( dataToSend ).replace(/\"/g, '');
    
    ajax.getDataCall("http://11.11.11.11/url/index.php?",dataToSend,successCallBack,failureCallback)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 streamingtool
  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析