복붙노트

[JQUERY] URL 인코딩 AJAX를 요청 jQuery를 문자열

JQUERY

URL 인코딩 AJAX를 요청 jQuery를 문자열

해결법


  1. 1.에 encodeURIComponent를보십시오.

    에 encodeURIComponent를보십시오.

    예:

    var encoded = encodeURIComponent(str);
    

  2. 2.에 encodeURIComponent 나를 위해 잘 작동합니다. 우리는 다음과 같이 아약스 call.The 코드에서이 같은 URL을 제공 할 수 있습니다 :

    에 encodeURIComponent 나를 위해 잘 작동합니다. 우리는 다음과 같이 아약스 call.The 코드에서이 같은 URL을 제공 할 수 있습니다 :

      $.ajax({
        cache: false,
        type: "POST",
        url: "http://atandra.mivamerchantdev.com//mm5/json.mvc?Store_Code=ATA&Function=Module&Module_Code=thub_connector&Module_Function=THUB_Request",
        data: "strChannelName=" + $('#txtupdstorename').val() + "&ServiceUrl=" + encodeURIComponent($('#txtupdserviceurl').val()),
        dataType: "HTML",
        success: function (data) {
        },
        error: function (xhr, ajaxOptions, thrownError) {
        }
      });
    

  3. 3.더 좋은 방법:

    더 좋은 방법:

    에 encodeURIComponent는 다음을 제외한 모든 문자를 이스케이프 : 알파벳, 소수점 숫자, - _. ! ~ * '()

    서버에 예상치 못한 요청을 방지하기 위해, 당신은 URI의 한 부분으로 전달됩니다 사용자가 입력 매개 변수에 encodeURIComponent를 호출해야합니다. 예를 들어, 사용자는 변수 코멘트 "백리향 및 시간이 다시 ="입력 할 수 있습니다. 다시 = 코멘트 = 시간 백리향의 20 %를 줄 것이다이 변수에 encodeURIComponent를 사용하지 않습니다. 앰퍼샌드와 등호가 새로운 키와 값 쌍을 표시합니다. 그래서 대신 POST 코멘트 키를 갖는 당신은 다시 동일 하나는 "백리향"다른 (시간)에 동일이 POST 키가 "다시 = 백리향 및 시간"에 동일.

    애플리케이션의 / X-WWW는 형태-urlencoded를 http://www.w3.org/TR/html401/interac...m-content-type 당 (POST), 공간이므로, '+'로 대체되어야 하나는 "+"와 "% 20"의 추가 교체와에 encodeURIComponent 교체를 수행 할 수 있습니다.

    하나의 소원은 RFC 3986을 준수에 더 엄격한을 할 수있는 경우 (이 보유 ', (,), 그리고 *!),이 문자는 어떤 용도를 구분 URI를 공식화에도 불구하고, 다음 안전하게 사용할 수 없습니다 :

    function fixedEncodeURIComponent (str) {
      return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
    }
    

  4. 4.당신이 URL은 하드 코딩보다 직접적으로 다른 PARAMS을 통과 할 때 백 엔드로 MVC3 / EntityFramework를 사용하고, 프런트 엔드를 직접 게시, JQuery와 통해 내 프로젝트의 모든 컨트롤러를 소비 ($를 사용하여 .post) 나던, 데이터 encription이 필요합니다. 나는 이미 내가 심지어 매개 변수로 URL (이 http://www.ihackforfun.eu/index.php?title=update-on-url-crazy&more=1&c=1&tb=1&pb=1)를 보내 여러 문자를 테스트했다 당신은 URL 내에서의 모든 데이터를 통과 할 때 전혀 문제에 encodeURIComponent가 잘 작동하지 비록 (하드 코딩)

    당신이 URL은 하드 코딩보다 직접적으로 다른 PARAMS을 통과 할 때 백 엔드로 MVC3 / EntityFramework를 사용하고, 프런트 엔드를 직접 게시, JQuery와 통해 내 프로젝트의 모든 컨트롤러를 소비 ($를 사용하여 .post) 나던, 데이터 encription이 필요합니다. 나는 이미 내가 심지어 매개 변수로 URL (이 http://www.ihackforfun.eu/index.php?title=update-on-url-crazy&more=1&c=1&tb=1&pb=1)를 보내 여러 문자를 테스트했다 당신은 URL 내에서의 모든 데이터를 통과 할 때 전혀 문제에 encodeURIComponent가 잘 작동하지 비록 (하드 코딩)

    금지 된 URL 즉>

     var encodedName = encodeURIComponent(name);
     var url = "ControllerName/ActionName/" + encodedName + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;; // + name + "/" + keyword + "/" + description + "/" + linkUrl + "/" + includeMetrics + "/" + typeTask + "/" + project + "/" + userCreated + "/" + userModified + "/" + status + "/" + parent;
    

    그렇지 않으면 그나마 사용에 encodeURIComponent 대신은 아약스 POST 메서드 내에서 PARAMS 전달 시도

     var url = "ControllerName/ActionName/";   
     $.post(url,
            { name: nameVal, fkKeyword: keyword, description: descriptionVal, linkUrl: linkUrlVal, includeMetrics: includeMetricsVal, FKTypeTask: typeTask, FKProject: project, FKUserCreated: userCreated, FKUserModified: userModified, FKStatus: status, FKParent: parent },
     function (data) {.......});
    

  5. 5.이걸로 해봐

    이걸로 해봐

    var query = "{% url accounts.views.instasearch  %}?q=" + $('#tags').val().replace(/ /g, '+');
    
  6. from https://stackoverflow.com/questions/6544564/url-encode-a-string-in-jquery-for-an-ajax-request by cc-by-sa and MIT license