Moeez Agha
Moeez Agha

Reputation: 217

Syntax error in Javascript function

Java script

$('#senurl').click(function () {

    $.ajax({
        type: "POST",
        url: "/Admin/Coupon1/Reject",
        dataType: "json",
        data:  "id="[email protected]+"&url="+@url

    });
});

ReferenceError: Expired is not defined
[Break On This Error]
data: "id="+2925+"&url="+Expired

Upvotes: 0

Views: 75

Answers (3)

Muthuram
Muthuram

Reputation: 154

$('#senurl').click(function () {

$.ajax({
    type: "POST",
    url: "/Admin/Coupon1/Reject",
    dataType: "json",
data: "{id:'" + @Model.id + "', 'url': " + @url + "}",  
   success: function (response) {
    alert( response.d); 
},
error: function (data) {
    alert(data);
},
failure: function (msg) {

}
});

});

Try this it is working fine. If you are using url routing then you might get some other error. so better get the respone output and check..

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075925

You probably want (but see also below):

$('#senurl').click(function () {

    $.ajax({
        type: "POST",
        url: "/Admin/Coupon1/Reject",
        dataType: "json",
        data:  "[email protected]&url=@url"

    });
});

...because you have to think about what the browser sees, and if @url gets replaced with Expired by the server, from the error you can tell that what the browser sees for your code is:

data: "id="+2925+"&url="+Expired // <=== What the browser sees with your current code

Even better, let jQuery handle any potential URI-encoding needed by passing it an object instead:

$('#senurl').click(function () {

    $.ajax({
        type: "POST",
        url: "/Admin/Coupon1/Reject",
        dataType: "json",
        data:  {id: @Model.id, url: "@url"}
    });
});

If you don't want to pass jQuery an object and let it handle the URI-encoding for you, then you'll want to handle it yourself:

data:  "[email protected]&url=" + encodeURIComponent("@url")

Upvotes: 2

Harish Ambady
Harish Ambady

Reputation: 13171

I think its because @url variable is assigned data Expired without quotes.

Upvotes: -1

Related Questions