jQuery Ajax网址评估

I have to fire an ajax call against two different domain based on the platform of the client (mobile/desktop):

var baseDomain = (isMobile()) ? "http://m.test.com" : "http://www.test.com";

function AddProduct(ProductId, ButtonClientId) {
    $.ajax({
        type : "POST",
        eval("url : \""+baseDomain+"/data/call.aspx/AddToShoppingCart\",");
        contentType : "application/json; charset=utf-8",
        data : '{productId:' + ProductId + ', quantity: 1, isSingle: true}',
        dataType : "json",
        success : function(data) {
            ProductAddedSuccess(data.d, ButtonClientId);
        },
        error : ProductAddedError
    });
}

I cannot go thru this as I always get the "SyntaxError: missing formal parameter". Where I'm wrong?

The URL is just a string, so create the string you want in a variable and assign it to the url property of the Ajax options:

var baseDomain = isMobile() ? "http://m.test.com" : "http://www.test.com";

function AddProduct(ProductId, ButtonClientId) {
    $.ajax({
        type : "POST",
        url: baseDomain + "/data/call.aspx/AddToShoppingCart",
        contentType : "application/json; charset=utf-8",
        data : '{productId:' + ProductId + ', quantity: 1, isSingle: true}',
        dataType : "json",
        success : function(data) {
            ProductAddedSuccess(data.d, ButtonClientId);
        },
        error : ProductAddedError
    });
}

The reason for the error is you are placing a function call (to eval()) in the middle of an anonymous object declaration!

e.g.

{
   propName1: "Value 1",
   someFunctionCall(),
   propName2: "Value 3"
 }

Which makes no sense to Javascript :)

Try this:

$.ajax({
    type : "POST",
    url : baseDomain + "/data/call.aspx/AddToShoppingCart",
    contentType : "application/json; charset=utf-8",
    data : '{productId:' + ProductId + ', quantity: 1, isSingle: true}',
    dataType : "json",
    success : function(data) {
        ProductAddedSuccess(data.d, ButtonClientId);
    },
    error : ProductAddedError
});