如何使用javscript中的GET方法在ajax调用中传递数据

时间:2018-05-28 12:13:34

标签: javascript ajax qunit

我正在使用qunit在javascript中编写单元测试用例。我使用ajax调用并使用GET方法调用一个URL,但它不调用URL。我在下面提供测试:

 QUnit.test( "Importing Grid", function(assert) {
    var done = assert.async();    
    var data = {
        "info": {"view":"LATEST","mode": 1,"memberId": 1001,"baselineId": -1}
    }
    console.log(cuboid_id+" : "+cuboid_name);
    $.ajax({
        url: Globals.baseURL + "rest/grid/"+cuboid_id,
        type: "GET",
        dataType: "application/json",
        data: JSON.stringify(data),
        contentType: "application/json",
        success: function(result){                
            console.log(JSON.stringify(result));                                                       
            assert.equal(result !=null,true,"Response should not be null");               
            assert.equal(result[0].error,"Whitebaord ID NOT FOUND","InValid Whiteboard ID");               
            done();
        }
    });
});

有人可以建议我在ajax呼叫中改变什么? 我试过this stackoverflow answer but it is not working 在此先感谢。

1 个答案:

答案 0 :(得分:1)

试试这个

$.ajax({
    url: Globals.baseURL + "rest/grid/"+cuboid_id,
    type: "GET",
    dataType: "application/json",
    data: {some_query_var : JSON.stringify(data)},
    contentType: "application/json",
    success: function(result){
        console.log("***********************++++++++++++++*************************");
        console.log(JSON.stringify(result));                                                       
        //assert.equal(result !=null,true,"Response should not be null");               
        //assert.equal(result[0].error,"Whitebaord ID NOT FOUND","InValid Whiteboard ID");
        assert.equal(1,1); 
        done();
    }
});

" dataType json"在ajax中,jquery并不意味着在"数据"中形成JSON字符串。属性.. 你仍然需要传递"数据"属性。在这种情况下,我使用some_query_var。