-1

I am using jQuery to call get ajax "Uncaught SyntaxError: Unexpected identifier" at line 3.

In here, i removed the original URL due to confidential purpose. but even i remove the csrHost line, the following line also will hit the same error.

Please help. Thanks in advance.

function getCSR(){
    $.ajax ({
        var csrHost = "https://example.com/jsp/csr.jsp?";
        var commonname = '&'+'commonname'+'='+$('#csrName').val().toUpperCase();
        var company = '&'+'org'+'='+$('#csrCom').val().toUpperCase();
        var city = '&'+'locality'+'='+$('#csrCity').val().toUpperCase();
        var state = '&'+'state'+'='+$('#csrState').val().toUpperCase();
        var country = '&'+'country'+'='+$('#csrCountry').val().toUpperCase();
        var OrgUnit = '&'+'commonname'+'='+$('#csrOU').val().toUpperCase();
        var keysize = '&'+'keysize'+'='+2048;
        url: csrHost+commonname+company+city+state+country+OrgUnit+keysize,
        type: "GET",
        //data: {latlng:40.714224,-73.961452}
        //contentType: "application/json; charset=utf-8",
        success: function(csr){
            var textareaResult = $('#textarea1')

            textareaResult.html(csr)

        }
    })
}

5 Answers 5

4

Instead of jQuery-slim you need to use standard jQuery. ajax is removed from the jQuery-slim version.

You need to create the url outside of the ajax function. It takes an object as parameter, but you use var inside it.

function getCSR() {

    var csrHost = "https://example.com/jsp/csr.jsp?";
    var commonname = '&' + 'commonname' + '=' + $('#csrName').val().toUpperCase();
    var company = '&' + 'org' + '=' + $('#csrCom').val().toUpperCase();
    var city = '&' + 'locality' + '=' + $('#csrCity').val().toUpperCase();
    var state = '&' + 'state' + '=' + $('#csrState').val().toUpperCase();
    var country = '&' + 'country' + '=' + $('#csrCountry').val().toUpperCase();
    var OrgUnit = '&' + 'commonname' + '=' + $('#csrOU').val().toUpperCase();
    var keysize = '&' + 'keysize' + '=' + 2048;

    $.ajax ({
        url: csrHost + commonname + company + city + state + country + OrgUnit + keysize,
        type: "GET",
        //data: { latlng: 40.714224, -73.961452 },
        //contentType: "application/json; charset=utf-8",
        success: function(csr) {
            var textareaResult = $('#textarea1');
            textareaResult.html(csr)
        }
    })
}

You can also try with this approach, which I think is more beautiful.

function getCSR() {

    var csrHost = "https://example.com/jsp/csr.jsp?";

    var keyValues = [
      { name: 'commonname', value: $('#csrName').val() },
      { name: 'org', value: $('#csrCom').val() },
      { name: 'locality', value: $('#csrCity').val() },
      { name: 'state', value: $('#csrState').val() },
      { name: 'country', value: $('#csrCountry').val() },
      { name: 'commonname', value: $('#csrOU').val() },
      { name: 'keysize', value: '2048' },
    ];

    var queryString = keyValues.map(item => `${item.name}=${item.value.toUpperCase()}`)
                               .join('&');       

        $.ajax ({
            url: csrHost + queryString,
            type: "GET",
            //data: { latlng: 40.714224, -73.961452 },
            //contentType: "application/json; charset=utf-8",
            success: function(csr) {
                var textareaResult = $('#textarea1');
                textareaResult.html(csr)
            }
        })
    }
5
  • Thanks. But now i got another error "Uncaught TypeError: $.ajax is not a function" from the $.ajax({ line.
    – Jerry
    Nov 27, 2017 at 8:08
  • 1
    Have you add jQuery? To what refers your $ ? Nov 27, 2017 at 8:10
  • End of my html is using <script type="text/javascript" language="javascript" src="code.jquery.com/jquery-3.2.1.slim.js"></script> <script type="text/javascript" language="javascript" src="script.js"></script> For my script.js, it just the above jquery code.
    – Jerry
    Nov 27, 2017 at 8:12
  • 1
    $.ajax is removed from the slim version. Use standard jQuery Nov 27, 2017 at 8:16
  • Thank you so much, it working now.. Although i still got error but i think need to create another post for that..
    – Jerry
    Nov 27, 2017 at 8:38
0

The Definition of Jquery Ajax function defines as

jQuery.ajax( url [, settings ] )

which means you can supply a url and settings (optional). you are trying to define a bunch of statements, which are invalid in this context and hence the error. You need to pass in an URL or a settings object as specified by the Spec. Jquery Ajax Specification

For example in your case following would be the right way :

function getCSR() {

    var csrHost = "https://example.com/jsp/csr.jsp?";
    var commonname = '&' + 'commonname' + '=' + $('#csrName').val().toUpperCase();
    var company = '&' + 'org' + '=' + $('#csrCom').val().toUpperCase();
    var city = '&' + 'locality' + '=' + $('#csrCity').val().toUpperCase();
    var state = '&' + 'state' + '=' + $('#csrState').val().toUpperCase();
    var country = '&' + 'country' + '=' + $('#csrCountry').val().toUpperCase();
    var OrgUnit = '&' + 'commonname' + '=' + $('#csrOU').val().toUpperCase();
    var keysize = '&' + 'keysize' + '=' + 2048;

    $.ajax ({
        url: csrHost + commonname + company + city + state + country + OrgUnit + keysize,
        type: "GET",
        //data: { latlng: 40.714224, -73.961452 },
        //contentType: "application/json; charset=utf-8",
        success: function(csr) {
            var textareaResult = $('#textarea1');
            textareaResult.html(csr)
        }
    })
}
0

You need to extracts the creation of variables from within the ajax function. Like this:

function getCSR(){  
    var csrHost = "https://example.com/jsp/csr.jsp?",
        commonname = '&'+'commonname'+'='+$('#csrName').val().toUpperCase(),
        company = '&'+'org'+'='+$('#csrCom').val().toUpperCase(),
        city = '&'+'locality'+'='+$('#csrCity').val().toUpperCase(),
        state = '&'+'state'+'='+$('#csrState').val().toUpperCase(),
        country = '&'+'country'+'='+$('#csrCountry').val().toUpperCase(),
        OrgUnit = '&'+'commonname'+'='+$('#csrOU').val().toUpperCase(),
        keysize = '&'+'keysize'+'='+2048,
        url = csrHost+commonname+company+city+state+country+OrgUnit+keysize
    $.ajax ({
        url: url,
        type: "GET",
        success: function(csr){
            var textareaResult = $('#textarea1')
            textareaResult.html(csr)
        }
    })
}

More info about JQuery Ajax function's here.

0

Hiii Actually the problem is , you can not use var inside ajax request ,You can define those variables outside the $.ajax and then use those inside it , it will work.

and just to let you know , you can not use var inside json object :)

{
"key":"value"
} 

This is the correct way of json

$.ajax( {    }   )

this holds a json object so this can not contain the var.

hope it will clear your doubts :) :)

0
function getCSR(){
    var csrHost = "https://example.com/jsp/csr.jsp";
    var param = {
            commonname: $('#csrName').val().toUpperCase(), 
            org: $('#csrCom').val().toUpperCase(), 
            locality: $('#csrCity').val().toUpperCase(), 
            state: $('#csrState').val().toUpperCase(),
            country: $('#csrCountry').val().toUpperCase(),
            commonname: $('#csrOU').val().toUpperCase(),
            keysize:2048
            };

    $.ajax ({
        url: csrHost,
        type: "GET",
        data: param,
        //contentType: "application/json; charset=utf-8",
        success: function(csr){
            var textareaResult = $('#textarea1')
            textareaResult.html(csr)
        }
    })
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.