开发者

How to change AJAX call target when calling from different view/page?

I am testing a .js script using Qunit.

I have a static .html test harness that includes the .js "code under test" (CUT) and contains appropriate markup. I am testing using QUnit in another script that is included.

This harness and the test script resides in a project: Application.UI.Tests.JScript.

There are AJAX calls in the CUT that have a target relative to the location of the view that uses it.

The views and scripts reside in the conventional folders in an MVC project: Application.UI.

The build event of Application.UI.Tests.JScript does a Del and XCopy of the Scripts and Content folders to ensure the tests are running against a copy of the la开发者_JS百科test version of the CUT.

Example code:

sendDataToServerViaAjax: function () {
    var dataToSend = somefunctionThatReturnsData();

    $.ajax({
         url: 'actionName'
        , type: 'post'
        , data: dataToSend
        , dataType: 'json'
        , success: function (returnedData) {
            alert('This was received by server: ' + returnedData);
        }
    });
}

The actionName goes to an action:

[ValidateInput(false)] //TODO for now
public JsonResult actionName(FormCollection formIn)
{
    string jsonData = formIn[0];
    JsonResult result = new JsonResult();
    result.Data = jsonData;
    return result;
}

which just returns the data for now and will eventually have calls to proper logic in the Domain layer.


Question: How can I make the CUT call the AJAX action on the controller in the running Application.UI project from the static .html page in Application.UI.Tests.JScript without editing the CUT to include information specific for testing?


Changing the url value from actionName to /Controller/actionName will make the url relative to the root of the site e.g.

$.ajax({
     url: 'actionName'
    , type: 'post'
    , data: dataToSend
    , dataType: 'json'
    , success: function (returnedData) {
        alert('This was received by server: ' + returnedData);
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜