2016-09-19 75 views
1

適配器JS如何將參數傳遞給JS HTTP適配器在MobileFirst 8

function getCitiesByCountry(countryName) 

{ 

var request = 
    var request= 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <web:GetCitiesByCountry> 
     <!--Optional:--> 
     <web:CountryName>INDIA</web:CountryName> 
     </web:GetCitiesByCountry> 
    </soapenv:Body> 
</soapenv:Envelope>; 

var input = 

{ 
    method: 'post', 
    returnedContentType: 'xml', 
    path: '/globalweather.asmx', 
    body: { 
     content: request.toString(), 
     contentType: 'text/json; charset=utf-8' 
    } 
}; 
var result = MFP.Server.invokeHttp(input); 
return result.Envelope.Body; 
}; 

適配器XML

<displayName>JavaScriptSOAP</displayName> 
<description>JavaScriptSOAP</description> 
<connectivity> 
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> 
     <protocol>http</protocol> 
     <domain>www.webservicex.net</domain> 
     <port>80</port> 
    </connectionPolicy> 
</connectivity> 

<procedure name="getCitiesByCountry" secured ="false"/> 

Index.js

function submitRequest() 
{ 
    var resourceRequest = new    WLResourceRequest("adapters/JavaScriptSOAP/getCitiesByCountry", WLResourceRequest.POST); 
    resourceRequest.setQueryParameter("params", "['India']"); 
    resourceRequest.send().then(
    function(response) { 
     alert('response '+JSON.stringify(response.responseText)); 
    }, 
     function(response) { 

     alert("HTTP Failure "+JSON.stringify(response)); 
    } 
    ); 
    } 

我需要傳遞參數到http適配器它包含國家名稱。我在索引js文件中包含國家名稱。在預覽應用程序時,我收到錯誤代碼415,

而且在控制檯我收到

[AUDIT ] CWWKS1100A: Authentication did not succeed for user ID test. An invalid user ID or password was specified. 
[AUDIT ] CWWKS1100A: Authentication did not succeed for user ID test. An invalid user ID or password was specified. 
[AUDIT ] CWWKS1100A: Authentication did not succeed for user ID test. An invalid user ID or password was specified. 

我:

的Eclipse的Java EE IDE的Web開發。 版本:Mars.2版本(4.5.2) 版本ID:20160218-0600 Windows 7的

我有什麼認證給。如何克服錯誤?

+0

我無法理解的問題。什麼是調用此適配器的客戶端代碼? –

+0

Iam無法從客戶端代碼調用適配器(即index.js)。現在我也包含了index.js代碼。 – CodeWriter

+0

何時發生此錯誤?你提到 - 編譯。編譯什麼 - 你的應用程序? –

回答

0

您正在傳遞錯誤的參數。 params是一個數組,因此你的代碼看起來應該像

function submitRequest() { var resourceRequest = new WLResourceRequest("adapters/JavaScriptSOAP/getCitiesByCountry", WLResourceRequest.POST); resourceRequest.setQueryParameter("params", "['India']"); resourceRequest.send().then(function(response) { alert('response '+JSON.stringify(response.responseText)); }, function(response) { alert("HTTP Failure "+JSON.stringify(response)); }); } 
+0

@ Yoel ..我只收到同樣的錯誤。 – CodeWriter

0

嘗試添加安全=「假」的XML文件中的過程聲明:

<procedure name="getCitiesByCountry" secured="false"/> 
+0

@ shmulik.Still接收同樣的錯誤。我是否需要在config.xml文件中添加任何身份驗證? – CodeWriter

1

415是「不支持的媒體類型」。這意味着服務器決定您發送的請求不包含服務器認爲應該的數據類型。

你得到這個的原因是因爲你正在發送一個沒有正文和查詢參數的POST請求。你可以做什麼來解決這個問題是將POST請求改爲GET請求。只要改變

WLResourceRequest("adapters/JavaScriptSOAP/getCitiesByCountry", WLResourceRequest.POST); 

WLResourceRequest("adapters/JavaScriptSOAP/getCitiesByCountry", WLResourceRequest.GET);